①元组的特征是 圆括号和逗号
>>> tuple1=(1,)
>>> type(tuple1)
<class 'tuple'>
>>> tuple1=(1,)
>>> type(tuple1)
<class 'tuple'>
②元组的内容不会被轻易修改:
>>> tuple1 = (1,2,3,5,4,6,7,8)
>>> tuple1[2]=(33,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
③修改元组的方法:元组被修改之后原来的会被回收,相当于标签内容回收。
>>> tuple1 = (1,2,3,5,4,6,7,8)
>>> tuple1 = tuple1[2:] + (666,) + tuple1[:2]
>>> tuple1
(3, 5, 4, 6, 7, 8, 666, 1, 2)
④字符串 str ,修改原内容也会回收
>>> str = "tongge hen shuai!"
>>> str1 = str[:2] + 'tebie' + str[2:]
>>> str1
'totebiengge hen shuai!'
>>> str = str1
>>> str
'totebiengge hen shuai!'
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
讲解常用的几个类方法:
①capitalize :首字母大写
>>> str.capitalize()
'Totebiengge hen shuai!'
②casefold :整个字母改写为小写
>>> str.casefold()
'totebiengge hen shuai!'
③center :字符居中
>>> str.center(40)
' totebiengge hen shuai! '
④count :计数
>>> str.count('g',1,15)
2
⑤translate :替换
>>> str1.translate(str.maketrans('n','h'))
'fish fuck ho ho ho '二 格式化字符串:位置参数,关键字参数,两者混合参数
(位置参数必须在关键字参数之前)
>>> "{a} {b} {c}".format(a="通哥",b="哥",c="很帅")
'通哥 哥 很帅'
>>> "{0} {1} {2}".format("通哥","很","帅")
'通哥 很 帅'