python string字符串函数

python rstrip()函数用于去除字符串右边指定的字符。

与之相似的函数为python lstrip()函数,它是去除字符串左边指定的字符。

语法

str.rstrip(chars)

参数

  • chars:指要去除的字符  默认为空格或换行符。

返回值

返回去除指定字符新的字符串。

程序示例

#!/usr/bin/python
# coding=utf-8
str1 = "abcjabck123kluabcca"
print(str1.rstrip("abc"))  #只去掉右边的"abcca",右边第6个字符u不在"abc"中,去除结束
str2 = "12578asdfgh11112"
print(str2.rstrip("12")) #只去掉字符串str2右边的 11112 

程序运行结果:

abcjabck123klu
12578asdfgh