溫馨提示×

python的strip函數怎么使用

小億
101
2023-12-21 06:31:10
欄目: 編程語言

Python中的strip()函數用于去除字符串首尾的指定字符(默認為空格)。

函數語法:strip([chars])

其中,chars參數是可選的,用于指定要去除的字符。如果沒有指定chars參數,則默認去除字符串首尾的空格字符。

示例:

string = "   hello, world!   "
new_string = string.strip()
print(new_string)   # 輸出:hello, world!

string = "-----hello, world!-----"
new_string = string.strip('-')
print(new_string)   # 輸出:hello, world!

在第一個示例中,strip()函數去除了字符串首尾的空格字符。

在第二個示例中,strip(‘-’)函數去除了字符串首尾的連續的"-"字符。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女