如何用Python高效移除字符串中的空格和换行符?
- 内容介绍
- 文章标签
- 相关推荐
本文共计92个文字,预计阅读时间需要1分钟。
1. 去除空格:`strip()`
2.保留字符串:`xyz.strip()`
3.输出结果:`xyz`
一、去除空格
strip()" xyz ".strip() # returns "xyz"
" xyz ".lstrip() # returns "xyz "
" xyz ".rstrip() # returns " xyz"
" x y z ".replace(' ', '') # returns "xyz"
二、替换 replace("space","")
用replace("\n", ""),后边的串替换掉前边的
本文共计92个文字,预计阅读时间需要1分钟。
1. 去除空格:`strip()`
2.保留字符串:`xyz.strip()`
3.输出结果:`xyz`
一、去除空格
strip()" xyz ".strip() # returns "xyz"
" xyz ".lstrip() # returns "xyz "
" xyz ".rstrip() # returns " xyz"
" x y z ".replace(' ', '') # returns "xyz"
二、替换 replace("space","")
用replace("\n", ""),后边的串替换掉前边的

