Python String rstrip() 方法

Python String rstrip() 方法

rstrip() 删除 string 字符串末尾的指定字符,默认为空白符,包括空格、换行符、回车符、制表符。

Python String rstrip() 语法

rstrip()方法语法:

str.rstrip([chars])

Python String rstrip() 参数

  • chars - 指定删除的字符(默认为空白符)

Python String rstrip() 返回值

返回删除 string 字符串末尾的指定字符后生成的新字符串。

Python String rstrip() 实例1

以下实例展示了 rstrip() 函数的使用方法,从String的右边删除任何一个匹配的字符。
在这里,在Python字符串rstrip()函数的参数中,我们传递了一个有多个字符的字符串。在这种情况下,rstrip()将尝试从原始字符串的右侧剥离所提供的字符,只要这些字符与给定参数中的任何字符匹配。

#!/usr/bin/python3

string = "api for demos, ok?"

print(string.rstrip('skei'))

输出:

Python String rstrip() 方法

Python String rstrip() 实例2

使用rstrip()方法去除字符串中的尾部空白。

#!/usr/bin/python3

string = "apidemos.com     "
print(string.rstrip())

输出:

Python String rstrip() 方法

Python String rstrip() 实例3

Python rstrip()如果不能剥离所提供的字符,则返回原始字符串。

#!/usr/bin/python3

# string which is to be stripped
string = "api for demos.com"

# string doesn't have trailing 'j' or 'z', thus the rstrip() method didn't update the string
print(string.rstrip('jz'))

输出:

Python String rstrip() 方法

赞(1)
未经允许不得转载:极客笔记 » Python String rstrip() 方法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
Python OS模块
Python os.chown方法Python os.write() 方法Python os.pardir 方法
Python String模块
Python String capitalize()方法Python String count()方法Python String center()方法Python String expandtabs()方法Python String index()方法Python String isalnum()方法Python String endswith()方法Python String encode()方法Python String find() 方法Python String decode()方法Python String isalpha() 方法Python String isdigit() 方法Python String islower() 方法Python String isnumeric() 方法Python String isspace() 方法Python String istitle() 方法Python String isupper() 方法Python String join() 方法Python String len() 方法Python String ljust() 方法Python String lower() 方法Python String lstrip() 方法Python String maketrans() 方法Python String max() 方法Python String min() 方法Python String replace() 方法Python String rfind() 方法Python String rindex() 方法Python String rjust() 方法Python String rstrip() 方法Python String isdecimal() 方法Python String splitlines() 方法Python String split() 方法Python String startswith() 方法Python String swapcase() 方法Python String strip() 方法Python String translate() 方法Python String title() 方法Python String zfill() 方法Python String upper() 方法
Python Math 模块
Python Math exp() 函数Python Math ceil() 函数Python Math floor() 函数Python Math fabs() 函数Python Math log10() 函数Python Math log() 函数Python Math pow() 函数Python Math modf() 函数Python round() 函数Python Math sqrt() 函数Python Math acos() 函数Python Math asin() 函数Python Math atan() 函数Python Math atan2() 函数Python Math cos() 函数Python Math degrees() 函数Python Math hypot() 函数Python Math radians() 函数Python Math sin() 函数Python Math tan() 函数
Python Random 模块
Python random choice() 函数Python random random() 函数Python random randrange() 函数Python random seed() 函数Python random shuffle() 函数Python random uniform() 函数
Python List 模块
Python List min() 方法Python List len() 方法Python List list() 方法Python List max() 方法