Python字符串去除空格:trim() vs. replace()
Python中trim()方法只能去除字符串开头和结尾的空格,无法去除字符串中间的空格。如果要去除字符串中间的空格,可以使用replace()方法结合正则表达式来实现。例如:
string = " hello world "
new_string = string.replace(" ", "")
print(new_string)
输出结果为:"helloworld",可以看到中间的空格已经被成功去除了。
原文地址: https://www.cveoy.top/t/topic/p4w5 著作权归作者所有。请勿转载和采集!