python join
时间: 2023-10-13 21:17:27 浏览: 60
Python join()函数原理及使用方法
The `join()` method in Python is used to join the elements of a list or tuple into a single string, with a specified separator. It takes a sequence as an argument and returns a string.
Syntax:
```
string.join(iterable)
```
Here, `string` is the separator that you want to use to join the elements of the iterable, and `iterable` is the sequence of elements that you want to join.
Example:
```
fruits = ['apple', 'banana', 'orange']
separator = ', '
result = separator.join(fruits)
print(result)
```
Output:
```
apple, banana, orange
```
In this example, we have used the `join()` method to join the elements of the `fruits` list into a single string, separated by a comma and a space. The resulting string is stored in the `result` variable, which is then printed to the console.
阅读全文