1,字符串内容的对齐方式
s='hello,Python'
'''居中对齐'''
print(s.center(20,'*'))
'''左对齐'''
print(s.ljust(20,'*'))
print(s.ljust(10))
print(s.ljust(20))
'''右对齐'''
print(s.rjust(20,'*'))
print(s.rjust(20))
print(s.rjust(10))
'''右对齐,使用0进行填充'''
print(s.zfill(10))
print(s.zfill(20))
print('-2334'.zfill(8))
输出结果为:
****hello,Python****
hello,Python********
hello,Python
hello,Python
********hello,Python
hello,Python
hello,Python
hello,Python
00000000hello,Python
-0002334
2,字符串的劈分
s='hello world python'
lst=s.split()
print(lst)
s1='hello|world|python'
print(s1.split(sep='|'))
print(s1.split(sep='|',maxsplit=1))
print('----------------------------------')
print(s.rsplit())
print(s1.rsplit('|'))
print(s1.rsplit(sep='|',maxsplit=1))
输出结果为:
['hello', 'world', 'python']
['hello', 'world', 'python']
['hello', 'world|python']
----------------------------------
['hello', 'world', 'python']
['hello', 'world', 'python']
['hello|world', 'python']
3,字符串的判断
s='hello,python'
print('1.',s.isidentifier()) #False
print('2.','hello'.isidentifier()) #True
print('3.','张三'.isidentifier()) #True
print('4.','张三1'.isidentifier()) #True
print('5.','zhangsan_123'.isidentifier()) #True
print('6.','\t'.isspace()) #True
4,字符串的替换和合并
s='hello,python'
s1='hello,python,python,python'
print(s.replace('python','java'))
print(s1.replace('python','java',2))
lst=['hello','java','python']
print('|'.join(lst))
print(''.join(lst))
t=('hello','java','python')
print(''.join(t))
print('*'.join('python'))
输出结果为:
hello,java
hello,java,java,python
hello|java|python
hellojavapython
hellojavapython
p*y*t*h*o*n