Tutorial Study Image

Python lstrip()

Python 中的 lstrip() 函数根据给定的参数移除原始字符串副本中的前导字符。该方法返回此副本作为输出。


string.lstrip([chars]) #where chars are those to remove as leading characters
 

lstrip() 参数

lstrip() 函数将字符作为其参数。如果未提供字符,则从字符串中移除前导空格。

参数 描述 必需/可选
chars 要移除的前导字符 可选

lstrip() 返回值

返回值始终是一个字符串。直到找到第一个匹配项,所有字符组合都会从左侧开始移除。

输入 返回值
字符串 字符串副本

Python 中 lstrip() 方法的示例

示例 1:lstrip() 在 Python 中如何工作?


# Variable declaration  
string1 =  "  Python  "  
# Leading whitepsace are removed
print(string1.lstrip()) 
string2 =  ",,,,,ssaaww.....programming" 
print(string2.lstrip(",.asw"))  
 

输出


 Python  
programming 

示例 2:Istrip 的工作原理


# Variable declaration  
string1 =  "$$$$-Python-$$$$"  
# Calling function  
string2 = string1.lstrip('$')  
# Displaying result  
print(string1)  
print(string2)  
 

输出


$$$$-Python-$$$$
-Python-$$$$