python中的strip()
时间: 2023-11-21 20:55:26 浏览: 28
strip()是Python中的一个字符串方法,用于删除字符串开头和结尾的指定字符(默认为空格)。该方法返回一个新的字符串,原始字符串不会被修改。下面是一些例子:
1. 删除字符串开头和结尾的空格:
```python
str1 = " hello, world! "
print(str1.strip()) # 输出:hello, world!
```
2. 删除字符串开头和结尾的指定字符:
```python
str2 = "00000003210Runoob01230000000"
print(str2.strip('0')) # 输出:3210Runoob0123
```
3. 删除字符串开头的指定字符:
```python
str3 = "00000003210Runoob01230000000"
print(str3.lstrip('0')) # 输出:3210Runoob01230000000
```
4. 删除字符串结尾的指定字符:
```python
str4 = "00000003210Runoob01230000000"
print(str4.rstrip('0')) # 输出:00000003210Runoob0123
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)