python 字符串处理

s='abcdsh'
# 把字符串都转为大写 ,会产生一个新的字符串对象
print(s.upper(),id(s),id(s.upper()))

# 把字符串都转为小写,会产生一个新的字符串对象
b = 'hello,Python'
print(b.lower(),id(b),id(b.lower()))

# 大转小 小转大
f = 'hfhhHGSDG'
print(f.swapcase(),id(f),id(f.swapcase()))

#每个字母开头变为大写 其余变为小写
print(b.title())

ABCDSH 1875249815992 1875249850552
hello,python 1875250826480 1875253086128
HFHHhgsdg 1875253086064 1875253086128
Hello,Python

s='hello,Python'
# 居中对齐     字符串总宽度,填重复
print(s.center(20,'*'))
# 左对齐
print(s.ljust(20,'*'))
#右对齐
print(s.rjust(20,'*'))

#补0
print('-2137'.zfill(10))

****hello,Python****
hello,Python********
********hello,Python
-000002137

#字符串分割
s = 'hello world python'
print(s.split())

# 从左侧开始分割
s1 = 'hello|world|python'
print(s1.split(sep='|'))
print(s1.split(sep='|',maxsplit=1))
# 从右侧开始分割
print(s1.rsplit(sep='|'))
print(s1.rsplit(sep='|',maxsplit=1))

['hello', 'world', 'python']
['hello', 'world', 'python']
['hello', 'world|python']
['hello', 'world', 'python']
['hello|world', 'python']

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值