python_入门_第五天

元组:戴上了枷锁的列表

元组固定不能改变

创建元组

temp = () #空元组

>>> temp = (1) #单个元素没有逗号视为int
>>> type(temp)
<class 'int'>

>>> temp = (1,) #添加逗号才是元组
>>> type(temp)
<class 'tuple'>

元组更新

>>> temp = (1,2,3,4)
>>> temp
(1, 2, 3, 4)
>>> temp = temp[:2]+(3,)+temp[2:]
>>> temp
(1, 2, 3, 3, 4)

删除

del temp #删除元组temp

字符串

  • capitalize() 把字符串的第一个字符改为大写其它的改成小写
>>> str1='yueKe feiFei'
>>> str1.capitalize()
'Yueke feifei'
  • casefold() 把整个字符串的所有字符改为小写
>>> str1='yueKe feiFei'
>>> str1.casefold()=
'yueke feifei'
  • center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串
>>> str1='yueKe feiFei'
>>> str1.center(20)
'    yueKe feiFei    '
  • count(sub[, start[, end]]) 返回 sub 在字符串里边出现的次数,start 和 end 参数表示范围,可选。
>>> str1='yueKe feiFei'
>>> str1.count('e',0,7)
2
  • endswith(sub[, start[, end]])检查字符串是否以 sub 子字符串结束,如果是返回 True,否则返回 False。start 和 end 参数表示范围,可选。
>>> str1='yueKe feiFei'
>>> str1.endswith('fei')
True
  • expandtabs([tabsize=8])把字符串中的 tab 符号(\t)转换为空格,如不指定参数,默认的空格数是 tabsize=8。
>>> str1 = 'yueke\tfeifei'
>>>> str1.expandtabs(tabsize=8)
'yueke   feifei'
  • find(sub[, start[, end]]) 检测 sub 是否包含在字符串中,如果有则返回索引值,否则返回 -1,start 和 end 参数表示范围,可选。
>>> str1 = 'yueke feifei'
>>> str1.find('f'1,10)
6
  • index(sub[, start[, end]]) 跟 find 方法一样,不过如果 sub 不在 string 中会产生一个异常。
>>> str1 = 'yueke\tfeifei'
>>> str1.index('f')
6
>>> str1.index('w')
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    str1.index('w')
ValueError: substring not found
  • isalnum() 如果字符串至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False。
>>> str1 = '1yueke feifei'
>>> str1.isalnum()
False
>>> str1='1yuekefeifei'
>>> str1.isalnum()
True 
  • isalpha() 如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False。
>>> str1 = 'yueke feifei'
>>> str1.isalpha()
False
>>> str1='yuekefeifei'
>>> str1.isalpha()
True
  • isdecimal() 如果字符串只包含十进制数字则返回 True,否则返回 False。
>>> str1='yueke44'
>>> str1.isdecimal()
False
>>> str1='4444'
>>> str1.isdecimal()
True
  • isdigit() 如果字符串只包含数字则返回 True,否则返回 False。
>>> str1='yueke44'
>>> str1.isdigit()
False
>>> str1='4444'
>>> str1.isdigit()
True
  • islower() 如果字符串中至少包含一个区分大小写的字符,并且这些字符都是小写,则返回 True,否则返回 False。
>>> str1='asd123asd'
>>> str1.islower()
True
>>> str1='asdQ'
>>> str1.islower()
False
  • isnumeric() 如果字符串中只包含数字字符,则返回 True,否则返回 False。
>>> str1='12345'
>>> str1.isnumeric()
True
>>> str1='asd123'
>>> str1.isnumeric()
False
  • isspace() 如果字符串中只包含空格,则返回 True,否则返回 False。
>>> str1=' '
>>> str1.isspace()
True
  • istitle() 如果字符串是标题化(所有的单词都是以大写开始,其余字母均小写),则返回 True,否则返回 False。
>>> str1='Yueke feifei'
>>> str1.istitle()
False
>>> str1='Yueke Feifei'
>>> str1.istitle()
True
>>> 
  • isupper()如果字符串中至少包含一个区分大小写的字符,并且这些字符都是大写,则返回 True,否则返回 False。
>>> str1='yueke44'
>>> str1.isupper()
False
>>> str1='YUEKE44'
>>> str1.isupper()
True
  • join(sub)以字符串作为分隔符,插入到 sub 中所有的字符之间。
>>> str1='aa'
>>> str1.join('yueke')
'yaauaaeaakaae'
  • ljust(width) 返回一个左对齐的字符串,并使用空格填充至长度为 width 的新字符串。
>>> str1
'aaaaaa'
>>> str1.ljust(20)
'aaaaaa              '
  • lower() 转换字符串中所有大写字符为小写。
>>> str1='aaAAaa'
>>> str1.lower()
'aaaaaa'
  • lstrip() 去掉字符串左边的所有空格
>>> str1='   aaaa'
>>> str1.lstrip()
'aaaa'
  • partition(sub) 找到子字符串 sub,把字符串分成一个 3 元组 (pre_sub, sub, fol_sub),如果字符串中不包含 sub 则返回 (‘原字符串’, ”, ”)
>>> str1='yueke feifei'
>>> str1.partition('e')
('yu', 'e', 'ke feifei')
  • replace(old, new[, count]) 把字符串中的 old 子字符串替换成 new 子字符串,如果 count 指定,则替换不超过 count 次。
>>> str1='aaAAaa'
>>> str1.replace('aa','cc')
'ccAAcc'
>>> str1.replace('aa','cc',1)
'ccAAaa'
  • rfind(sub[, start[, end]]) 类似于 find() 方法,不过是从右边开始查找。
>>> str1='yueke feifei'
>>> str1.rfind('f')
9
  • rindex(sub[, start[, end]]) 类似于 index() 方法,不过是从右边开始。
>>> str1='yueke feifei'
>>> str1.rindex('f')
9
>>> str1.rindex('c')
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    str1.rindex('c')
ValueError: substring not found
  • rjust(width) 返回一个右对齐的字符串,并使用空格填充至长度为 width 的新字符串。
>>> str1='yueke feifei'
>>> str1.rjust(20)
'        yueke feifei'
  • rpartition(sub) 类似于 partition() 方法,不过是从右边开始查找。
>>> str1='yueke feifei'
>>> str1.rpartition('e')
('yueke feif', 'e', 'i')
  • rstrip() 删除字符串末尾的空格。
>>> str1='yueke feifei  '
>>> str1.rstrip()
'yueke feifei'
  • split(sep=None, maxsplit=-1) 不带参数默认是以空格为分隔符切片字符串,如果 maxsplit 参数有设置,则仅分隔 maxsplit 个子字符串,返回切片后的子字符串拼接的列表。
>>> str1='yueke feifei'
>>> str1.split()
['yueke', 'feifei']
  • splitlines(([keepends])) 在输出结果里是否去掉换行符,默认为 False,不包含换行符;如果为 True,则保留换行符
>>> str1='yueke \n feifei'
>>> str1.splitlines()
['yueke ', ' feifei']
>>> str1.splitlines(True)
['yueke \n', ' feifei']
  • startswith(prefix[, start[, end]]) 检查字符串是否以 prefix 开头,是则返回 True,否则返回 False。start 和 end 参数可以指定范围检查,可选。
>>> str1='yueke feifei'
>>> str1.startswith('y')
True
  • strip([chars]) 删除字符串前边和后边所有的空格,chars 参数可以定制删除的字符,可选。
>>> str1='  yueke feifei  '
>>> str1.strip()
'yueke feifei'
  • swapcase() 翻转字符串中的大小写。
>>> str1='YUEKE feifei'
>>> str1.swapcase()
'yueke FEIFEI'
  • title() 返回标题化(所有的单词都是以大写开始,其余字母均小写)的字符串。
>>> str1='yueke feifei'
>>> str1.title()
'Yueke Feifei'
  • translate(table) 根据 table 的规则(可以由 str.maketrans(‘a’, ‘b’) 定制)转换字符串中的字符。
>>> str1='yueke feifei'
>>> str1.translate(str.maketrans('f','G'))
'yueke GeiGei'
  • upper() 转换字符串中的所有小写字符为大写。
>>> str1='qqaass'
>>> str1.upper()
'QQAASS'
  • zfill(width) 返回长度为 width 的字符串,原字符串右对齐,前边用 0 填充。
>>> str1='qqaass'
>>> str1.zfill(20)
'00000000000000qqaass'
  • format() 位置参数,关键字参数
>>> "{0} love {1}.{2}".format('I','Love','yueke')#位置
'I love Love.yueke'
>>> '{a} love {b}'.format(a='I',b='GG')#关键字
'I love GG'
>>> '{0} love {a}'.format('I',a='GG')#共存但是关键字后不能有位置
'I love GG'

猜你喜欢

转载自blog.csdn.net/m0_37416991/article/details/81072062
今日推荐