python-字符串-技巧

1、删除字符串末尾空白:rstrip函数

test1 = "This is a test    "
print(test1.rstrip())

但是这种删除只是暂时的,如果想永久删除,则要将修改后的结果存回到变量中

test1 = "This is a test    "
test1 = test1.rstrip()
print(test1)

2、删除字符串前端或者字符串两边的空白:lstrip函数和strip函数

test1 = "   This is another test    "
test2 = test1.lstrip()#删除前端
test3 = test1.strip()#删除两端
print(test1)
print(test2)
print(test3)

 

posted on 2018-01-18 19:11  告辞  阅读(113)  评论(0)    收藏  举报

导航