English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python 基础教程

Python 流程控制

Funzione in Python

Tipi di dati in Python

Python 文件操作

Python 对象和类

Python 日期和时间

Python 高级知识

Python 参考手册

Python 字符串 lstrip() 使用方法及示例

Python 字符串方法

lstrip()方法用于删除字符串头部指定的字符,默认字符为所有空字符,包括空格、换行(\n)、制表符(\t)等。

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://it.oldtoolbag.com/'
print(website.lstrip('htps:/.'))

运行该程序时,输出为:

this is good 
   this is good 
his is good 
it.oldtoolbag.com/

Python 字符串方法