温馨提示×

python中lstrip怎么使用

小亿
92
2023-11-13 11:56:06
栏目: 编程语言

在Python中,lstrip()函数可以用于去除字符串开头的指定字符或字符序列。它的使用方法如下:

  1. 去除开头的空格:
str1 = "   hello world"
result = str1.lstrip()
print(result)  # 输出:hello world
  1. 去除开头的指定字符或字符序列:
str2 = "###hello world"
result = str2.lstrip("#")
print(result)  # 输出:hello world

str3 = "abcabcabc123"
result = str3.lstrip("abc")
print(result)  # 输出:123

注意:lstrip()函数只能去除字符串开头的字符,并不会对字符串中间或结尾的字符进行操作。

0