python 中的rstrip()
函數根據給定的參數刪除原始字元串副本中的尾隨字元。方法將此副本作為輸出返回。
**string.rstrip([chars])** #where chars are those to remove from right
rstrip()
參數:
函數的作用是:將一組字元作為參數。如果未提供字元,則從字元串中刪除尾隨空格。
參數 | 描述 | 必需/可選 |
---|---|---|
燒焦 | 要作為尾隨字元移除的字元 | 可選擇的 |
rstrip()
返回值
返回值始終是字元串。在找到第一個匹配之前,所有字元組合都將從右側開始移除。
| 投入 | 返回值 |
| 線 | 字元串的副本 |
Python 中rstrip()
方法的示例
示例rstrip()
在 Python 中是如何工作的?
string1 = " python "
string2 = string1.rstrip()
print("of all programming languages", string2, "is my favorite")
輸出:
of all programming languages python is my favorite
示例 2:在 Python 中使用rstrip()
# Variable declaration
string1 = "Python and Java* "
# Calling function
string2 = string1.rstrip(*)
# Displaying result
print("Before strip: ",string1)
print("After strip: ",string2)
輸出:
Before strip: Python and Java*
After strip: Python and Java
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297562.html