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

   2025-02-15 7720
核心提示:在Python中,可以使用字符串的strip()方法来去除字符串前后的空格。示例如下:s = hello worlds = s.strip()print(s)# 输出:hel

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

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

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

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

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言