Day8 字符串操作
------------------------------------------------------
product=[‘a’,’b’,’c’,’d’]
for index,item in enumerate(product):
print(index,item)
>>>
0 a
1 b
2 c
3 d
------------------------------------------------------
if a.isdigit():#判断是否为数字(1或‘1’)
\033[31:1m%s\033[0m #%s加颜色31
l 字符串操作:name=”My \t name is chen”
print(name.capitalize())#大写
print(name.count(“a”))
print(name.center(50,”_”))
print(name.endswith(“en”))#是否为指定字符结尾
print(name.expandtabs(tabsize=30))#tab 空格个数
print(name.find(“”))
print(name.format(name=’chen’,year=26))#格式化输出 name=”My name is {name} and i am {year} old”
print(name.format_map(_{‘name’:’chen’,’year’”26}))#格式化输出字典格式 name=”My name is {name} and i am {year} old”
print(name.islnum())#是否为A-Z,a-z,0-9
print(name.alphabet())#是否为A-Z,a-z
print(name.isdecimal())#是否为十进制
print(name.isdigit())#是否为整数
print(name.isidentifier())#判断是不是一个合法的标识符(变量名)
print(name.islower())#是否为小写
print(name.istitle())#是否首字母大写 “My Name Is”,是否为标题样式
print(name.isupper())#是否为大写
join:
s = ';'
li = ['apple', 'pear', 'orange']
fruit = s.join(li)
print fruit
>>> apple;pear;orange
print(name.ljust(50,’*”))#保证name长度为50,不够用*补全(右边)
print(name,rjust(50,’*’)) #保证name长度为50,不够用*补全(左边)
print(name.lower())#变小写
print(name.upper())#变大写
print(name.lstrip())#在左边去掉空格或回车
maketrans()
p=str.maketrans(“abcdef”,”123456”)
print(“chen”.translate(p))
>>>3h4n#把字符串转换为后面的值
print(‘chen’.replace(‘e’,’E’,1))>>>chEn
print(‘chen’.rfind(‘e’))#找到最右边的值的下标
split()
print(‘chen q1ang’.split(‘ ’))>>>[‘chen’,’q1ang’]
print(‘chen\n q1ang’.splitlines())#等于split(‘\n’) 按换行符分解
print(‘Chen’.swapcase())>>>cHEN
print(‘chen’.zfill(50))>>>00000000000000chen#十六进制时补位