Python rstrip() メソッド

Python 字符串Python 文字列


説明

Python rstrip() は string 文字列の末尾の指定された文字を削除します。デフォルトは空白文字で、スペース、改行文字、復帰文字、タブ文字を含みます。

構文

rstrip() メソッドの構文:

str.rstrip([chars])

パラメータ

  • chars -- 削除する文字を指定します(デフォルトは空白文字)

戻り値

string 文字列の末尾から指定した文字を削除した後に生成される新しい文字列を返します。

例

次の例は、rstrip() 関数の使用方法を示しています。

例

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

random_string = 'this is good    '

# 文字列末尾のスペースは削除されます
print(random_string.rstrip())

# 'si oo' は末尾の文字ではないため、何も削除されません
print(random_string.rstrip('si oo'))

# 'sid oo' では 'd oo' が末尾の文字であり、'ood' が文字列から削除されます
print(random_string.rstrip('sid oo'))

# 'm/' は末尾の文字です。'.' の末尾文字は見つからないため、'm/' が文字列から削除されます
website = 'www.example.com/'
print(website.rstrip('m/.'))


# カンマ(,)、ピリオド(.)、文字 s、q、w を削除します。これらはすべて末尾の文字です
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)
# 末尾の文字 * を削除します
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*'))
print(x)

上記の例の出力結果は次のとおりです:

this is good
this is good
this is g
www.サンプル.co
banana
*****this is string example....wow!!!

Python 字符串Python 文字列

その他の拡張