在Python中,strip()
函数用于去除字符串两端的指定字符(默认为空格)或空白符。可以使用以下方式来使用strip()
函数:
s = " hello world "
print(s.strip()) # Output: "hello world"
s = "!!!hello world!!!"
print(s.strip("!")) # Output: "hello world"
s = "!!!hello world!!!"
print(s.strip("!d")) # Output: "hello worl"
需要注意的是,strip()
函数会返回一个新的字符串,原始字符串不会被修改。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python中strip怎么使用