Python 字符串 rstrip() 方法

實例

刪除字符串右邊的空格:

txt = "     banana     "
x = txt.rstrip()
print("of all fruits", x, "is my favorite")

運行實例

定義和用法

rstrip() 方法刪除所有結尾字符(字符串末尾的字符),空格是要刪除的默認結尾字符。

語法

string.rstrip(characters)

參數值

參數 描述
characters 可選。一組作為結尾字符要刪除的字符。

更多實例

實例

刪除結尾字符:

txt = "banana,,,,,ssaaww....."
x = txt.rstrip(",.asw")
print(x)

運行實例