Python String | rstrip()

Python String rstrip() 方法

Python字符串rstrip()方法返回一个去除尾部字符的字符串的拷贝(基于传递的字符串参数)。如果没有传递参数,它将删除尾部的空格.

语法:

string.rstrip([chars])

参数:

chars (可选) – 一个字符串,指定要删除的字符集.

返回值:

返回字符串的副本,并去除尾部的字符

示例1

# Python3 program to demonstrate the use of
# rstrip() method using optional parameters
 
# string which is to be stripped
string = "geekssss"
 
# Removes given set of characters from
# right.
print(string.rstrip('s'))

输出:

geek

示例2

# Python3 program to demonstrate the use of
# rstrip() method using optional parameters
 
# string which is to be stripped
string = "   for    "
 
# Leading whitespaces are removed
print("Geeks" + string.rstrip() + " Geeks ")

输出:

Geeks   for Geeks

示例3

# string which is to be stripped
string = "geeks for geeks"
 
# Argument doesn't contain trailing 's'
# So, no characters are removed
print(string.rstrip('ek'))

输出:

geeks for geeks

示例4

# string which is to be stripped
string = "geeks for geeks"
 
# Removes given set of characters from
# right.
print(string.rstrip('ske'))

输出:

geeks for g

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程