字符串的应用(二)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值