有个列表['hello', 'world', 'python'],如何把列表里面的字符串联起来,得到字符串 “hello_world_python”? 输入样例1: 在这里给出一组输入。例如: ['hello', 'world', 'python'] 输出样例1: 在这里给出相应的输出。例如: hello_world_python 输入样例2: 在这里给出一组输入。例如: ['hi', 'python', 'man', 'world', '!'] 输出样例2: 在这里给出相应的输出。例如: hi_python_man_world_!
时间: 2023-12-01 09:04:22 浏览: 49
可以使用字符串的 `join()` 方法,将列表中所有字符串拼接起来,中间用下划线 `_` 隔开。
代码如下:
```python
lst = ['hello', 'world', 'python']
result = '_'.join(lst)
print(result)
# 输出:hello_world_python
```
```python
lst2 = ['hi', 'python', 'man', 'world', '!']
result2 = '_'.join(lst2)
print(result2)
# 输出:hi_python_man_world_!
```
相关问题
有个列表[hello,'world,python'],如何把列表里面的字符串联起来,得到字符串“hello world _python"?
可以使用字符串的 join 方法,代码如下:
```
lst = ['hello', 'world', 'python']
result = ' '.join(lst).replace('_', ' ')
print(result)
```
输出结果为:
```
hello world python
```
有个列表['hello','world','python'],如何把列表里面的字符串联起来,得到字符串“hello _world _python"?
可以使用字符串的 join 方法,代码如下:
```
lst = ['hello', 'world', 'python']
result = ' _'.join(lst)
print(result)
```
输出结果为:
```
hello _world _python
```