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!