lstrip()方法返回已除去前导字符的字符串的副本(基于传递的字符串参数)。如果未传递任何参数,它将删除前导空格。
用法: string.lstrip(characters)
参数:
characters[可选]:一组要删除的前导字符。
返回:返回去除前导字符的字符串副本。
代码1:
# Python3 program to demonstrate the use of
# lstrip() method using default parameter
# string which is to be stripped
string = " geeksforgeeks"
# Removes spaces from left.
print(string.lstrip())
输出:
geeksforgeeks
代码2:
# Python3 program to demonstrate the use of
# lstrip() method using optional parameters
# string which is to be stripped
string = "++++x...y!!z* geeksforgeeks"
# Removes given set of characters from left.
print(string.lstrip("+.!*xyz"))
输出:
geeksforgeeks
代码3:
# string which is to be stripped
string = "geeks for geeks"
# Argument doesn't contain leading 'g'
# So, no characters are removed
print(string.lstrip('ge'))
输出:
ks for geeks
代码4:运行时错误
当我们尝试剥离字符串以外的任何内容时,都会出现运行时错误。
# Python3 program to demonstrate the use of
# strip() method error
string = " geeks for geeks "
list =[1, 2, 3]
# prints the error message
print(list.lstrip())
输出:
print(list.lstrip()) AttributeError:'list' object has no attribute 'lstrip'
相关用法
- Numpy string lstrip()用法及代码示例
- Python string islower()用法及代码示例
- Python string isupper()用法及代码示例
- Python string capwords()用法及代码示例
注:本文由纯净天空筛选整理自AKASH GUPTA 6大神的英文原创作品 Python String | lstrip() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。