31个必备的Python字符串方法,建议收藏!

1、Slicing

s = '   hello   '
s = s[:]

print(s)
#    hello


s = '   hello   '
s = s[3:8]

print(s)
# hello

 

2、strip()

s = '   hello   '.strip()

print(s)
# hello


s = '###hello###'.strip()

print(s)
# ###hello###

3、lstrip()

s = '   hello   '.lstrip()

print(s)
# hello

 

4、rstrip()

s = '   hello   '.rstrip()

print(s)
#    hello

https://mp.weixin.qq.com/s/3ebqb72hz4wvBaIP0vk-0A

 

posted @ 2022-02-28 15:53  Bonnie_ξ  阅读(47)  评论(0编辑  收藏  举报