跳到主要内容

Python 字符串 lstrip() 方法

lstrip() 方法根据参数(指定要删除的字符集的字符串)从左侧移除字符。

lstrip() 的语法是:

string.lstrip([chars])

lstrip() 参数

  • chars(可选)- 指定要移除的字符集的字符串。

如果没有提供 chars 参数,则从字符串中移除所有前导空白字符。

lstrip() 返回值

lstrip() 返回一个副本,其中前导字符被剥离。

chars 参数中的所有字符组合都会从字符串的左侧被移除,直到第一个不匹配。

示例:lstrip() 的工作原理

random_string = ' this is good '

# 移除前导空白
print(random_string.lstrip())

# 参数不包含空格
# 没有字符被移除。
print(random_string.lstrip('sti'))

print(random_string.lstrip('s ti'))

website = 'https://www.mashangxue123.com/'
print(website.lstrip('htps:/.'))

输出

this is good
this is good
his is good
www.mashangxue123.com/