去除str中所有的空格
时间: 2023-04-06 15:05:04 浏览: 71
好的,可以使用Python中的replace()函数来去除字符串中的空格,代码如下:
```python
str = "Hello World! "
str = str.replace(" ", "")
print(str)
```
输出结果为:
```
HelloWorld!
```
希望能够帮到你!
相关问题
python 去除str中所有的空格
可以使用replace()方法将空格替换为空字符串,示例代码如下:
str = "hello world"
str = str.replace(" ", "")
print(str)
输出结果为:helloworld
去除变量str中的所有空格
可以使用 python 的字符串方法 `replace` 来去除字符串中的空格。
例如:
```
str = " this is a string with spaces "
str = str.replace(" ", "")
print(str) # 输出 "thisisastringwithspaces"
```
或者,也可以使用 python 的字符串方法 `strip` 来去除字符串两端的空格。
例如:
```
str = " this is a string with spaces "
str = str.strip()
print(str) # 输出 "this is a string with spaces"
```
也可以使用 `lstrip` 和 `rstrip` 方法分别去除字符串左端和右端的空格。