python 中的strip()
函數根據給定的參數刪除原始字元串副本中的尾隨和前導字元。方法將此副本作為輸出返回。
**string.strip([chars])** #where chars are those to remove from right & left
條帶()參數:
strip()
函數將一組字元作為其參數。如果未提供字元,則從字元串中刪除尾隨空格和前導空格。
參數 | 描述 | 必需/可選 |
---|---|---|
燒焦 | 要作為尾隨和前導字元移除的字元 | 可選擇的 |
條帶()返回值
返回值始終是字元串。在找到第一個匹配之前,它從字元串的左側和右側進行搜索,如果到達字元串的末尾,它將停止移除。
| 投入 | 返回值 |
| 線 | 字元串的副本 |
Python 中strip()
方法的示例
示例strip()
在 Python 中是如何工作的?
string1 = ",,,,,rrttgg.....python....rrr"
# Removing characters
string2 = string1.strip(",.grt")
print(string2)
輸出:
python
示例strip()
如何刪除兩邊的空白?
string1 = " Hii Python! "
# Removes white spaces
after_strip = string1.strip()
輸出:
Hii Python!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/232559.html