Python字串lstrip()方法

Python字串lstrip()方法返回一個字串的副本,其中指定字元(默認空白字元)從字串的左側刪除。

語法

以下是lstrip()方法的語法 -

str.lstrip([chars])

參數

  • chars - 這是要修剪的字元。

返回值

  • 此方法返回從字串開頭(左側)刪除所有指定字元(默認空白字元)後的字串。

示例

以下示例顯示了lstrip()方法的用法。

#!/usr/bin/python3
## 刪除左側字串:空格

str = "     this is string example....wow!!!"
print (str.lstrip())
## 刪除左側字串:*****
str = "*****this is string example....wow!!!*****"
print (str.lstrip('*'))

當運行上面的程式,它產生以下結果 -

this is string example....wow!!!
this is string example....wow!!!*****

上一篇: Python字串 下一篇: Python列表