Linux 拨号vps windows公众号手机端

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

lewis 8年前 (2017-08-18) 阅读数 12 #程序编程
文章标签 python

在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
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门