【Python 3.6.1】去掉字符串两端的空格
有的时候为了程序的健壮,需要将传入的字符串的两段空格去掉,python也有自己的方法去除两段空格。测试如下:
一、去掉两端空格strip()
|
1 2 3 4 5 6 |
>>> s1 = " hello " >>> print ("#"+s1+"#") # hello # >>> print ("#"+s1.strip()+"#") #hello# >>> |
二、去掉左侧空格lstrip()
|
1 2 3 4 5 6 |
>>> s2 = " hello" >>> print ("#"+s2+"#") # hello# >>> print ("#"+s2.lstrip()+"#") #hello# >>> |
三、去掉右侧空格rstrip()
|
1 2 3 4 5 6 |
>>> s3 = "hello " >>> print ("#"+s3+"#") #hello # >>> print ("#"+s3.rstrip()+"#") #hello# >>> |