提示:本篇包含rstrip()方法、strip()方法
文章目录
一、rstrip()方法
1.1 用途描述
rstrip() 删除 string 字符串末尾的指定字符(默认为空格)
1.2 语法
str.rstrip([chars]);
参数:chars -指删除的字符;
返回值:是返回删除 string 字符串末尾的指定字符后生成的新字符串。
1.3 实例
str = " this is a apple! "
print (str.rstrip())
str = "*****this is a apple!*****"
print (str.rstrip('*'))
------------------------------------------------------
## 输出为:
this is a apple!
*****this is a apple!
二、strip()方法
2.1 用途描述
strip() 方法:用于移除字符串头尾指定的字符(默认为空格)或字符序列。注:只能删除开头或是结尾的字符,不能删除中间部分的字符。
2.2 语法
str.strip([chars]);
参数:chars --移除字符串头尾指定的字符序列;
返回值:返回移除字符串头尾指定的字符序列生成的新字符串。
2.3 实例
str = "*****this is **a ** apple!*****"
print (str.strip('*'))
---------------------------------
## 输出为:
this is **a ** apple!
三、lstrip() 方法
3.1 用途描述
用于截掉字符串左边的空格或指定字符
3.2 语法
str.lstrip([chars])
参数:chars -指定截取的字符;
返回值:返回截掉字符串左边的空格或指定字符后生成的新字符串。
3.3 实例
str = " this is a apple! ";
print( str.lstrip() );
str = "*******this is a apple!*******";
print( str.lstrip("*") );
--------------------------------------------
## 输出为:
this is a apple!
this is a apple!*******
总结
新学的两个字符串的操作方法,记录一下,可用于数据的预处理。