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 @   Bonnie_ξ  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示