Python3 rstrip() メソッド

Python3 字符串Python3 文字列


説明

rstrip() は string 文字列の末尾の指定文字を削除します。デフォルトは空白文字で、スペース、改行文字、キャリッジリターン文字、タブ文字を含みます。

構文

rstrip() メソッドの構文:

str.rstrip([chars])

パラメータ

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

戻り値

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

インスタンス

以下のインスタンスは rstrip() 関数の使用方法を示しています:

インスタンス

#!/usr/bin/python3

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('*'))

上記のインスタンスの出力結果は次のとおりです:

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

Python3 字符串Python3 文字列

その他の拡張