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"
python删除字符串首尾空格函数

原文地址: https://www.cveoy.top/t/topic/fEBo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录