溫馨提示×

python右邊空白問題如何刪除

小億
87
2023-11-07 10:35:56
欄目: 編程語言

在Python中,可以使用strip()方法來刪除字符串兩邊的空白字符。
示例代碼:

string = "   Hello, World!   "
new_string = string.strip()
print(new_string)

輸出結果:

Hello, World!

strip()方法將刪除字符串兩邊的空白字符,包括空格、制表符和換行符。如果只想刪除右邊的空白字符,可以使用rstrip()方法。
示例代碼:

string = "   Hello, World!   "
new_string = string.rstrip()
print(new_string)

輸出結果:

  Hello, World!

注意,strip()和rstrip()方法返回的是刪除空白字符后的新字符串,并不會修改原來的字符串。如果想修改原來的字符串,可以將新字符串賦值回原來的變量。

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