Phương thức chuỗi string.join(iterable)2 trả về một chuỗi bằng cách nối tất cả các phần tử của một điều đáng tin (danh sách, chuỗi, tuple), được phân tách bởi dấu phân cách đã cho.
Thí dụ
text = ['Python', 'is', 'a', 'fun', 'programming', 'language']# join elements of text with space print(' '.join(text))
# Output: Python is a fun programming languageCú pháp của chuỗi tham gia ()
Cú pháp của phương thức string.join(iterable)2 là:
string.join(iterable)tham gia () tham số
Phương thức string.join(iterable)2 lấy một số khác (các đối tượng có khả năng trả lại các thành viên của mình một lần) làm tham số của nó.
Một số ví dụ về Iterables là:
- Các loại dữ liệu gốc - Danh sách, Tuple, Chuỗi, Từ điển và Set.
- Đối tượng tệp và đối tượng bạn xác định bằng phương thức string.join(iterable)5 hoặc string.join(iterable)6.
Lưu ý: Phương pháp string.join(iterable)2 cung cấp một cách linh hoạt để tạo các chuỗi từ các đối tượng có thể. Nó tham gia vào từng phần tử của một ITBER (như danh sách, chuỗi và tuple) bằng một dấu phân cách chuỗi (chuỗi mà phương thức string.join(iterable)2 được gọi) và trả về chuỗi được nối.: The string.join(iterable)2 method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the string.join(iterable)2 method is called) and returns the concatenated string.
Trả về giá trị từ tham gia ()
Phương thức string.join(iterable)2 trả về một chuỗi được tạo bằng cách nối các phần tử của một phân tách chuỗi đã cho.
Nếu có thể xác định được bất kỳ giá trị không chuỗi nào, nó sẽ tăng ngoại lệ # .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
0.Ví dụ 1: Hoạt động của phương thức tham gia ()
# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
Đầu ra
1, 2, 3, 4 1, 2, 3, 4 s1.join(s2): 1abc2abc3 s2.join(s1): a123b123cVí dụ 2: Phương thức tham gia () với các bộ
# .join() with sets test = {'2', '1', '3'} s = ', 'print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
Đầu ra
2, 3, 1 Python->->Ruby->->JavaVí dụ 2: Phương thức tham gia () với các bộ A set is an unordered collection of items, so you may get different output (order is random).
Lưu ý: Một bộ là một bộ sưu tập các mục không có thứ tự, vì vậy bạn có thể nhận được đầu ra khác nhau (thứ tự là ngẫu nhiên).
# .join() with dictionaries test = {'mat': 1, 'that': 2} s = '->' # joins the keys onlyprint(s.join(test))
test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't stringprint(s.join(test))
Đầu ra
Ví dụ 2: Phương thức tham gia () với các bộLưu ý: Một bộ là một bộ sưu tập các mục không có thứ tự, vì vậy bạn có thể nhận được đầu ra khác nhau (thứ tự là ngẫu nhiên).
Ví dụ 3: Phương thức tham gia () với từ điển: If the key of the string is not a string, it raises the # .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
0 exception.Python tham gia () trả về một chuỗi mới là sự kết hợp của các chuỗi khác trong điều kiện được chỉ định. Chúng tôi sẽ hiểu thêm về hàm python tham gia () trong bài viết dưới đây. 6.1 Q1.
Tại sao chúng ta sử dụng tham gia trong Python?
Phương thức python tham gia () được sử dụng để kết hợp một chuỗi với đối tượng có thể lặp lại. Nó trả về một chuỗi mới là sự kết hợp của các chuỗi trong Itable. Nó ném một loại ngoại lệ nếu có thể có bất kỳ giá trị không chuỗi nào. Nó cho phép các vòng lặp khác nhau như: danh sách, tuple, chuỗi, v.v.
Python tham gia () trả về một chuỗi mới là sự kết hợp của các chuỗi khác trong điều kiện được chỉ định. Chúng tôi sẽ hiểu thêm về hàm python tham gia () trong bài viết dưới đây. 6.1 Q1.
Tại sao chúng ta sử dụng tham gia trong Python?
Phương thức python tham gia () được sử dụng để kết hợp một chuỗi với đối tượng có thể lặp lại. Nó trả về một chuỗi mới là sự kết hợp của các chuỗi trong Itable. Nó ném một loại ngoại lệ nếu có thể có bất kỳ giá trị không chuỗi nào. Nó cho phép các vòng lặp khác nhau như: danh sách, tuple, chuỗi, v.v.
Làm cách nào để tham gia mã Python? is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.
Python chuỗi tham gia () Phương thức chuỗi tham gia () trả về một chuỗi bằng cách tham gia tất cả các phần tử của một điều đáng tin (danh sách, chuỗi, tuple), được phân tách bởi dấu phân cách đã cho. string_name.join(iterable)
Parameters:
- Xem thảo luậnList, Tuple, String, Dictionary, and Set
Cải thiện bài viết The join() method returns a string concatenated with the elements of iterable.
Lưu bài viết: If the iterable contains any non-string values, it raises a TypeError exception.
Đọcan empty string
Bàn luận
Python3
tham gia () là một hàm chuỗi sẵn có trong python được sử dụng để nối các phần tử của chuỗi được phân tách bằng một dấu phân cách chuỗi. Hàm này tham gia các phần tử của một chuỗi và làm cho nó trở thành một chuỗi. & Nbsp;
1, 2, 3, 4 1, 2, 3, 4 s1.join(s2): 1abc2abc3 s2.join(s1): a123b123c61, 2, 3, 4 1, 2, 3, 4 s1.join(s2): 1abc2abc3 s2.join(s1): a123b123c7
Cú pháp: String_name.join (ITBELBE) & NBSP;
1, 2, 3, 4 1, 2, 3, 4 s1.join(s2): 1abc2abc3 s2.join(s1): a123b123c6# .join() with sets test = {'2', '1', '3'} s = ', '
print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
2# .join() with sets test = {'2', '1', '3'} s = ', 'print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
3# .join() with sets test = {'2', '1', '3'} s = ', 'print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
4Output:
geeks $g$e$e$k$s$Có thể lặp lại - các đối tượng có khả năng trả lại các thành viên của họ một lần. Một số ví dụ là danh sách, tuple, chuỗi, từ điển và bộJoining String with lists using join()
Giá trị trả về: Phương thức tham gia () trả về một chuỗi được nối với các phần tử của Itable. & Nbsp;
Python3
# .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
3# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 # .join() with sets test = {'2', '1', '3'} s = ', 'print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
2# .join() with sets test = {'2', '1', '3'} s = ', 'print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
8# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java0# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java227____2, 3, 1 Python->->Ruby->->Java42, 3, 1 Python->->Ruby->->Java52, 3, 1 Python->->Ruby->->Java6# .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 2, 3, 1 Python->->Ruby->->Java82, 3, 1 Python->->Ruby->->Java6# .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 # .join() with dictionaries test = {'mat': 1, 'that': 2} s = '->' # joins the keys onlyprint(s.join(test))
test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't stringprint(s.join(test))
11, 2, 3, 4 1, 2, 3, 4 s1.join(s2): 1abc2abc3 s2.join(s1): a123b123c6# .join() with dictionaries test = {'mat': 1, 'that': 2} s = '->' # joins the keys only
print(s.join(test))
test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't stringprint(s.join(test))
3Output:
1-2-3-4Ví dụ 3: Tham gia chuỗi với các bộ bằng cách sử dụng tham gia ()Joining String with sets using join()
Trong ví dụ này, chúng tôi đang sử dụng một bộ python để tham gia chuỗi.
Lưu ý: Đặt chỉ chứa giá trị duy nhất do đó trong hai 4 một 4 được in.4 one 4 is printed.
Python3
# .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
3# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 # .join() with dictionaries test = {'mat': 1, 'that': 2} s = '->' # joins the keys onlyprint(s.join(test))
test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't stringprint(s.join(test))
6# .join() with sets test = {'2', '1', '3'} s = ', 'print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
8# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java0# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java227____2, 3, 1 Python->->Ruby->->Java4# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java4__2, 3, 1 Python->->Ruby->->Java6# .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 mat->that Traceback (most recent call last): File "...", line 12, in <module> TypeError: sequence item 0: expected str instance, int found92, 3, 1 Python->->Ruby->->Java6# .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 # .join() with dictionaries test = {'mat': 1, 'that': 2} s = '->' # joins the keys onlyprint(s.join(test))
test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't stringprint(s.join(test))
11, 2, 3, 4 1, 2, 3, 4 s1.join(s2): 1abc2abc3 s2.join(s1): a123b123c6# .join() with dictionaries test = {'mat': 1, 'that': 2} s = '->' # joins the keys only
print(s.join(test))
test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't stringprint(s.join(test))
3Output:
string.join(iterable)0Ví dụ 3: Tham gia chuỗi với các bộ bằng cách sử dụng tham gia ()
Trong ví dụ này, chúng tôi đang sử dụng một bộ python để tham gia chuỗi.
Python3
Lưu ý: Đặt chỉ chứa giá trị duy nhất do đó trong hai 4 một 4 được in.
# .join() with lists numList = ['1', '2', '3', '4'] separator = ', '
print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
3# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 # .join() with dictionaries test = {'mat': 1, 'that': 2} s = '->' # joins the keys onlyprint(s.join(test))
test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't stringprint(s.join(test))
6# .join() with sets test = {'2', '1', '3'} s = ', 'print(s.join(test))
test = {'Python', 'Java', 'Ruby'} s = '->->'print(s.join(test))
8# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java0# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java227____2, 3, 1 Python->->Ruby->->Java4# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
72, 3, 1 Python->->Ruby->->Java4__1, 2, 3, 4 1, 2, 3, 4 s1.join(s2): 1abc2abc3 s2.join(s1): a123b123c6string.join(iterable)05
Output:
2, 3, 1 Python->->Ruby->->Java6# .join() with lists numList = ['1', '2', '3', '4'] separator = ', 'print(separator.join(numList))
# .join() with tuples numTuple = ('1', '2', '3', '4')print(separator.join(numTuple))
s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'print('s1.join(s2):', s1.join(s2))
# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'print('s2.join(s1):', s2.join(s1))
4 mat->that Traceback (most recent call last): File "...", line 12, in <module> TypeError: sequence item 0: expected str instance, int found9