◼ lstrip()
时间: 2023-12-19 13:05:11 浏览: 21
lstrip() 是 Python 字符串方法之一,用于去除字符串左侧的空格或指定字符。例如:
```python
s = " hello world "
s = s.lstrip()
print(s) # 输出:'hello world '
```
在上面的例子中,lstrip() 方法去除了字符串 s 左侧的空格。
相关问题
pythonlstrip
Python中的lstrip()方法是用于移除字符串左侧指定的字符序列。 它可以去除字符串开头的指定字符,返回删除字符后的新字符串。这个方法通常用于去除字符串开头的空白符(如空格、制表符、换行符等)。
例如,对于字符串"pythonlstrip",如果我们使用lstrip()方法并给定参数"py",那么它将删除开头的"py",返回新字符串"thonlstrip"。如果不给定参数,默认删除开头的空白符。
下面是一个示例:
```python
a = " pythonlstrip"
print(a.lstrip()) # 输出结果为"pythonlstrip"
```
在这个示例中,字符串"a"的开头有三个空格,使用lstrip()方法去除开头的空格后,返回新字符串"pythonlstrip"。
python+lstrip
lstrip()方法用于去除字符串左侧的空格或指定字符。下面是一个例子:
```python
str = " hello world "
print(str.lstrip()) # 输出:'hello world '
```
在上面的例子中,lstrip()方法去除了字符串左侧的空格。如果想要去除指定字符,可以在lstrip()方法中传入一个参数,例如:
```python
str = "0000000hello world0000000"
print(str.lstrip('0')) # 输出:'hello world0000000'
```
在上面的例子中,lstrip()方法去除了字符串左侧的所有0。