主要参考 Stackoverflow答案总结。
空白字符一般指以下几种字符: space,tab, linefeed, return, formfeed, and vertical tab中英文对照表如下
去除空格
去除左右两边的空格使用str.strip():
去除所有空格
使用str.replace():
去除空白符
去除所有的空白符
使用str.splite()及join
使用正则
使用str.translate()
只去除左边的空白字符
使用str.lstrip()
使用正则
只去除右边的空白字符
使用str.rstrip()
使用正则
仅去除重复的空白页
使用正则
综上,str. strip()会移除字符串中开头与结束(左右两侧)的空白字符,中间部分的空白字符是不会移除的。
strip方法中可自定义要移除的字符,如下面这个示例,移除的为字符串中两侧的逗号