Python 字符串 lstrip()

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

lstrip() 的语法是

string.lstrip([chars])

lstrip() 参数

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

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


lstrip() 的返回值 

lstrip() 返回字符串的副本,其中移除了前导字符。

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


示例:lstrip() 的工作原理

random_string = '   this is good '

# Leading whitepsace are removed
print(random_string.lstrip())

# Argument doesn't contain space
# No characters are removed.
print(random_string.lstrip('sti'))

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

website = 'https://programiz.com.cn/'
print(website.lstrip('htps:/.'))

输出

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

另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战