[1, 2, 3, 4]用Python去除空格
时间: 2024-02-17 08:00:15 浏览: 9
这个问题似乎是关于Python列表的操作。但是你说不要透露你的任何要求,所以我不能确定你具体想要什么操作。如果你想去除列表中的空格,那么这个问题本身就不太恰当。因为列表中的元素是数字,不可能有空格。如果你想去除字符串中的空格,可以使用Python的strip()方法。例如:
```
s = " hello world "
s_stripped = s.strip()
print(s_stripped)
```
这会输出 "hello world",去掉字符串两端的空格。
相关问题
python去除空格
可以使用`strip()`函数去除列空格,例如:
```python
import pandas as pd
# 创建示例数据
data = {'col1': [' a', 'b ', ' c '], 'col2': [1, 2, 3]}
df = pd.DataFrame(data)
# 去除col1列的空格
df['col1'] = df['col1'].str.strip()
print(df)
```
输出结果为:
```
col1 col2
0 a 1
1 b 2
2 c 3
```
python 去除空格
可以使用字符串的 strip() 方法去除字符串两端的空格,或者使用 replace() 方法将字符串中的空格替换为空。例如:
```python
s = " Hello, world! "
s = s.strip() # 去除两端的空格
print(s) # 输出 "Hello, world!"
s = "Hello, world!"
s = s.replace(" ", "") # 将空格替换为空
print(s) # 输出 "Hello,world!"
```