python strip'
时间: 2023-10-15 12:31:32 浏览: 31
Python中的strip()方法是用来删除字符串前后(左右两侧)的空格或特殊字符的。
例如,对于字符串 str = 'hello world',使用str.strip('heldo').strip(),会先删除字符串的起始和结尾的'h'、'e'、'l'、'd'、'o'字符,然后再删除起始和结尾的空格。最终的结果是'wor'。
具体执行流程如下:
print str.strip('h') # 输出:'ello world'
print str.strip('h').strip('e') # 输出:'llo world'
print str.strip('h').strip('e').strip('l') # 输出:'o word'
print str.strip('h').strip('e').strip('l').strip('d') # 输出:'o wor'
print str.strip('h').strip('e').strip('l').strip('d').strip('o') # 输出:' w'
print str.strip('h').strip('e').strip('l').strip('d').strip('o').strip('l') # 输出:' w'
print str.strip('h').strip('e').strip('l').strip('d').strip('o').strip('l').strip() # 输出:'w' <span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python中strip函数几种用法](https://blog.csdn.net/lanhuazui10/article/details/119984549)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)