「Python」rstripメソッドで末尾の文字列を削除する

環境
PyCharm 2021.3
Python 3.9.7

書式
文字列.rstrip(削除する文字列)
文字列の末尾の文字を削除します
末尾の指定した文字列を削除し、文字列を返しています。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding: utf-8
str = "STUDY*SKILL"
res = str.rstrip("ILL")
print('末尾の文字列を削除する結果1')
print(res)
print('***************************')
strB = "東京都"
res2 = strB.rstrip("都")
print('末尾の文字列を削除する結果2')
print(res2)
# coding: utf-8 str = "STUDY*SKILL" res = str.rstrip("ILL") print('末尾の文字列を削除する結果1') print(res) print('***************************') strB = "東京都" res2 = strB.rstrip("都") print('末尾の文字列を削除する結果2') print(res2)
# coding: utf-8
str = "STUDY*SKILL"
res = str.rstrip("ILL")
print('末尾の文字列を削除する結果1')
print(res)

print('***************************')
strB = "東京都"
res2 = strB.rstrip("都")
print('末尾の文字列を削除する結果2')
print(res2)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
末尾の文字列を削除する結果1
STUDY*SK
***************************
末尾の文字列を削除する結果2
東京都
末尾の文字列を削除する結果1 STUDY*SK *************************** 末尾の文字列を削除する結果2 東京都
末尾の文字列を削除する結果1
STUDY*SK
***************************
末尾の文字列を削除する結果2
東京都

 

Python

Posted by arkgame