python去掉字符川中的空格
时间: 2023-11-08 09:53:14 浏览: 23
可以使用字符串的replace()方法来去掉字符串中的空格。例如:
```python
string_with_spaces = "hello world"
string_without_spaces = string_with_spaces.replace(" ", "")
print(string_without_spaces) # 输出"helloworld"
```
另外,还可以使用正则表达式来去掉字符串中的空格。例如:
```python
import re
string_with_spaces = "hello world"
string_without_spaces = re.sub(r"\s", "", string_with_spaces)
print(string_without_spaces) # 输出"helloworld"
```
其中,正则表达式`\s`表示匹配空格字符。`re.sub()`函数用于替换匹配到的字符串。