移除字符串头、尾指定的字符strip() rstrip()

1. Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)

#!/usr/bin/python
# -*- coding: UTF-8 -*-

str = "0000000     Runoob  0000000"; 
print str.strip( '0' );  # 去除首尾字符 0
str2 = "   Runoob      ";   # 去除首尾空格
print str2.strip();
# 以上实例输出结果如下:
     Runoob  
Runoob

2. Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格)

#!/usr/bin/python

str = "     this is string example....wow!!!     ";
print str.rstrip();
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8');
# 输出结果如下:
     this is string example....wow!!!
88888888this is string example....wow!!!

猜你喜欢

转载自blog.csdn.net/sinat_25873421/article/details/80217961