温馨提示×

python怎么去除字符串前后空格

小亿
94
2023-11-29 13:07:19
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Python中,可以使用字符串的strip()方法来去除字符串前后的空格。示例如下:

s = "  hello world  "
s = s.strip()
print(s)  # 输出:hello world

另外,如果只想去除字符串前面的空格,可以使用lstrip()方法;如果只想去除字符串后面的空格,可以使用rstrip()方法。示例如下:

s = "  hello world  "
s = s.lstrip()
print(s)  # 输出:hello world

s = "  hello world  "
s = s.rstrip()
print(s)  # 输出:  hello world

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:Python怎么去除字符串首尾空格

0