字符串
print('chenxi'*8)
测试
D:pythonpython.exe D:/untitled/dir/for.py chenxichenxichenxichenxichenxichenxichenxichenxi Process finished with exit code 0
字符串切片打印处理
print('chenxi'[2:])
测试
D:pythonpython.exe D:/untitled/dir/for.py enxi
判断字符串里有没有包含对应得字符
print('x'in 'chenxi') print('f'in 'chenxi')
测试
True False
字符串拼接,不建议使用这种方法
a='cx' b='zrd' c=a+b print(c)
测试
D:pythonpython.exe D:/untitled/dir/for.py cxzrd Process finished with exit code 0
字符串拼接
a='cx' b='zrd' c=''.join([a,b]) print(c)
测试
D:pythonpython.exe D:/untitled/dir/for.py cxzrd Process finished with exit code 0
字符串拼接
a='cx' b='zrd' bb='haha' c='-----'.join([a,b,bb]) print(c)
测试
D:pythonpython.exe D:/untitled/dir/for.py cx-----zrd-----haha
字符串之统计关键字个数
dis = 'hebei tianjing guanzhou' print(dis.count('i'))
测试
D:pythonpython.exe D:/untitled/dir/for.py 3
字符串之修改首字母为大写
dis = 'hebei tianjing guanzhou' print(dis.capitalize())
测试
D:pythonpython.exe D:/untitled/dir/for.py Hebei tianjing guanzhou
一共打印50字符,不够用指定字符去补,原先字符串内容居中
dis = 'hebei tianjing guanzhou' print(dis.center(50,'#'))
测试
D:pythonpython.exe D:/untitled/dir/for.py #############hebei tianjing guanzhou############## Process finished with exit code 0
判断字符串是不是以什么结尾的
dis = 'hebei tianjing guanzhou' print(dis.endswith('u')) print(dis.endswith('i'))
测试
D:pythonpython.exe D:/untitled/dir/for.py True False Process finished with exit code 0
判断字符串是不是以什么开头的
dis = 'hebei tianjing guanzhou' print(dis.startswith('he'))
测试
D:pythonpython.exe D:/untitled/dir/for.py True Process finished with exit code 0
修改字符串里tab的默认空格数量 表示tab键
dis = 'he bei tianjing guanzhou' print(dis.expandtabs(tabsize=10))
测试
D:pythonpython.exe D:/untitled/dir/for.py he bei tianjing guanzhou Process finished with exit code 0
找到字符串里第一个元素定返回索引值
dis = 'hebei tianjing guanzhou' print(dis.find('a'))
测试
D:pythonpython.exe D:/untitled/dir/for.py 8
将字符串变量赋值打印
dis = 'hebei tianjing guanzhou {name}' print(dis.format(name='zrd'))
测试
D:pythonpython.exe D:/untitled/dir/for.py hebei tianjing guanzhou zrd Process finished with exit code 0
以字典方式批量给字符串赋值
dis = 'hebei tianjing guanzhou {name} ll {age}' print(dis.format_map({'name':'zrd','age':22}))
测试
D:pythonpython.exe D:/untitled/dir/for.py hebei tianjing guanzhou zrd ll 22 Process finished with exit code 0
查字符串里关键字并返回索引值,字符串里没有关键字报错
dis = 'hebei tianjing guanzhou {name} ll {age}' print(dis.index('i')) print(dis.index('d'))
测试
D:pythonpython.exe D:/untitled/dir/for.py Traceback (most recent call last): File "D:/untitled/dir/for.py", line 170, in <module> print(dis.index('d')) ValueError: substring not found 4
判断字符串是否包含特殊字符
print('test564'.isalnum() ) print('tygc*'.isalnum()) print('reswd'.isalnum()) print('7890'.isalnum()) print('宋氏家族'.isalnum())
测试
D:pythonpython.exe D:/untitled/dir/for.py True False True True True
判断字符串像不像十进制的数字
print("3455".isdecimal()) print("程序".isdecimal()) print("AF09".isdecimal()) print("hevb".isdecimal())
测试
D:pythonpython.exe D:/untitled/dir/for.py True False False False
判断字符串是否包含一个非法字符
print("dtfghvh".isidentifier()) print("1233tyghgvh".isidentifier()) #非法字符
测试
D:pythonpython.exe D:/untitled/dir/for.py True False
判断字符是不是空格
print(' '.isspace())
测试
D:pythonpython.exe D:/untitled/dir/for.py True Process finished with exit code 0
判断字符串是不是标题格式
print('My Ch'.istitle()) print('My ch'.istitle())
测试
True False
字符串里所有大写改成小写
print('My Ch'.lower())
测试
D:pythonpython.exe D:/untitled/dir/for.py my ch Process finished with exit code 0
字符串所有小写该大写
print('My Ch'.upper())
测试
D:pythonpython.exe D:/untitled/dir/for.py MY CH Process finished with exit code 0
字符串中大小写翻转
print('My Ch'.swapcase())
测试
D:pythonpython.exe D:/untitled/dir/for.py mY cH Process finished with exit code 0
在字符串后面以特定字符补够特定数量
print('My Ch'.ljust(50,'*'))
测试
D:pythonpython.exe D:/untitled/dir/for.py My Ch********************************************* Process finished with exit code 0
在字符串前面以特定字符补够特定数量
print('My Ch'.rjust(50,'*'))
测试
D:pythonpython.exe D:/untitled/dir/for.py *********************************************My Ch Process finished with exit code 0
将字符串前后空格去掉
print(' My Ch'.strip())
测试
D:pythonpython.exe D:/untitled/dir/for.py My Ch Process finished with exit code 0
字符串内容替换
print('chenxi ffff'.replace('ffff','zrd'))
测试
D:pythonpython.exe D:/untitled/dir/for.py chenxi zrd Process finished with exit code 0
字符串内容替换的次数控制
print('chenxi ffff'.replace('ffff','zrd')) print('chenxi ffff tygdf'.replace('ff','zrd')) print('chenxi ffff tygdf'.replace('ffff','zrd',1))
测试
D:pythonpython.exe D:/untitled/dir/for.py chenxi zrd chenxi zrdzrd tygdf chenxi zrd tygdf Process finished with exit code 0
查字符串最后一个关键字在第几个索引
print('chenxi cx xlc'.rfind('x'))
测试
D:pythonpython.exe D:/untitled/dir/for.py 10 Process finished with exit code 0
将字符串以空格为分隔符,分成列表
print('chenxi cx xlc'.split(' '))
测试
D:pythonpython.exe D:/untitled/dir/for.py ['chenxi', 'cx', 'xlc'] Process finished with exit code 0