#元组 元组是一种不可变序列 它使用圆括号来表示:
a=(1,2,3)
print(a[0]) # 1
#元组是不可变的 不能对它进行赋值操作
#空元组: 创建一个空元组可以用没有内容的圆括号表示:
b=()
print(b) #输出:()
#!!!!!一个值的元组 需要在值后边再加一个逗号 这个比较特殊
c=(21,)
print(c)
print(type(c)) #<class 'tuple'>
d=(12)
print(d) #输出:12 本质上不是一个元组 只是用括号括起来 不是元组 是d=12
print(type(d)) #<class 'int'>
#元组操作:元组也是一种序列 因此也可以对同事进行索引 分片等 但是它不可变
##1.元组不可变不可变
#2.创建一个值的元组需要在值后边再加一个逗号
#字符串:字符串也是一种序列:跟元组一样也是不可变的
s='hello'
print(s[0]) # h 索引 index
print(s[1:3]) # el 分片 spilt ”左包右不包“
print(s + 'world') #helloworld 加法
print(s * 2) #hellohello 乘法
#注意:!!!字符串和元组一样 也是不可变的 所以不能进行赋值等操作
#字符串除了本身的一些通用的序列操作 还有自己的方法:
# join lower upper 等
#常用方法:find split join strip replace translate lower/upper
#具体使用:
#find 用于在一个字符串种查找子串 它返回子串所在位置的最左端索引 如果没有找到则返回-1
motto="to be or not to be ,that is a question"
print(motto.find('be')) # 返回‘b’的位置 索引位置为:3
print(motto.find('be', 4))
#指定从起始位置开始找 ,指定从第二个‘be’开始查找 并且返回该索引位置 :16
print(motto.find('be', 4, 7))#指定起始位置和终止位置 从4开始到7索引位置结束,没有找到则返回 -1
#split方法用于将字符串分割
str='/user/bin/ssh'
print(str.split('/')) #['', 'user', 'bin', 'ssh'] #若没有提供分隔符 则使用默认的空格分割
#join方法是split的逆方法 它用于将序列中的元素连接起来
str2=str.split('/')
print('/'.join(str2)) #/user/bin/ssh
#print('+'.join(1, 2, 3, 4, 5)) 不能是数字要是字符串
#strip用于移除字符串左右两侧的空格 但不包括内部,当然也可以指定需要移除的字符串
print(' hello world! '.strip()) #移除左右两侧空格
print('#### hello world! %%%%'.strip('%#')) #移除左右两侧的#或者%
#replace方法用于替换字符串中的所有匹配项
print(motto.replace('to','TO',2)) #将to替换成TO替换两次 替换结果: TO be or not TO be ,that is a question
print(motto)#原字符串不变 to be or not to be ,that is a question
#translate 和replace方法类似,替换字符串但是用于替换单个字符
#lower/upper 用于返回字符串的大写或者小写形式
x='PYTHON'
print(x.lower())
y='python'
print(y.upper())
#小结:1.字符串是不可变对象 调用对象自身的任意方法,也不会改变该对象的内容,相反 这些方
法会创建新的对象并返回
#2.translate 针对单个字符进行替换