Tôi có một danh sách và một chuỗi:
fruits = ['banana', 'apple', 'plum']
mystr = 'i like the following fruits: '
Làm thế nào tôi có thể kết hợp chúng để tôi nhận được
'i like the following fruits: banana, apple, plum'
Hãy nhớ rằng enum có thể thay đổi kích thước.
Gino Mempin
21.7K25 Huy hiệu vàng88 Huy hiệu bạc116 Huy hiệu đồng25 gold badges88 silver badges116 bronze badges
Hỏi ngày 28 tháng 9 năm 2012 lúc 2:59Sep 28, 2012 at 2:59
Tham gia danh sách, sau đó thêm các chuỗi.
print mystr + ', '.join[fruits]
Và không sử dụng tên của một loại tích hợp [
print mystr + ', '.join[fruits]
5] làm tên biến.Đã trả lời ngày 28 tháng 9 năm 2012 lúc 3:01Sep 28, 2012 at 3:01
Bạn có thể sử dụng mã này,
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Mã trên sẽ trả về đầu ra như dưới đây:
i like the following fruits: banana, apple, plum, pineapple, cherry
Đã trả lời ngày 27 tháng 12 năm 2013 lúc 8:53Dec 27, 2013 at 8:53
MBGSUIRPMBGSUIRPmbgsuirp
6191 Huy hiệu vàng7 Huy hiệu bạc 20 Huy hiệu đồng1 gold badge7 silver badges20 bronze badges
Bạn có thể sử dụng
print mystr + ', '.join[fruits]
6.result = "i like the following fruits: "+', '.join[fruits]
[Giả sử
print mystr + ', '.join[fruits]
7 chỉ chứa chuỗi]. Nếu print mystr + ', '.join[fruits]
7 chứa một chuỗi không, bạn có thể chuyển đổi nó một cách dễ dàng bằng cách tạo biểu thức máy phát trên đường bay:', '.join[str[f] for f in fruits]
Đã trả lời ngày 28 tháng 9 năm 2012 lúc 3:01Sep 28, 2012 at 3:01
Bạn có thể sử dụng mã này,mgilson
62 gold badges610 silver badges677 bronze badgesfruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Mã trên sẽ trả về đầu ra như dưới đây:
s = s + ', '.join[[str[fruit] for fruit in fruits]]
Đã trả lời ngày 27 tháng 12 năm 2013 lúc 8:53Sep 28, 2012 at 3:02
MBGSUIRPMBGSUIRPMark Ransom
6191 Huy hiệu vàng7 Huy hiệu bạc 20 Huy hiệu đồng40 gold badges384 silver badges610 bronze badges
0
Bạn có thể sử dụng
print mystr + ', '.join[fruits]
6.print[mystr, fruits]
[Giả sử
print mystr + ', '.join[fruits]
7 chỉ chứa chuỗi]. Nếu print mystr + ', '.join[fruits]
7 chứa một chuỗi không, bạn có thể chuyển đổi nó một cách dễ dàng bằng cách tạo biểu thức máy phát trên đường bay:Mgilsonmgilson11 gold badges35 silver badges48 bronze badges
290K62 Huy hiệu vàng610 Huy hiệu bạc677 Huy hiệu đồngApr 21, 2020 at 0:56
Bài viết này mô tả cách kết hợp các chuỗi trong Python.
- Concatenate nhiều chuỗi:
9,print mystr + ', '.join[fruits]
0 Nhà điều hànhfruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry'] mystr = 'i like the following fruits: ' print [mystr + ', '.join[fruits]]
- Các chuỗi và số Concatenate:
9,print mystr + ', '.join[fruits]
0 toán tử,fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry'] mystr = 'i like the following fruits: ' print [mystr + ', '.join[fruits]]
3,fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry'] mystr = 'i like the following fruits: ' print [mystr + ', '.join[fruits]]
4, F-Stringfruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry'] mystr = 'i like the following fruits: ' print [mystr + ', '.join[fruits]]
- Concatenate một danh sách các chuỗi thành một chuỗi:
5fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry'] mystr = 'i like the following fruits: ' print [mystr + ', '.join[fruits]]
- Concatenat một danh sách các số vào một chuỗi:
5,fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry'] mystr = 'i like the following fruits: ' print [mystr + ', '.join[fruits]]
3fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry'] mystr = 'i like the following fruits: ' print [mystr + ', '.join[fruits]]
Concatenate nhiều chuỗi: print mystr + ', '.join[fruits]
9, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
0 Nhà điều hành
print mystr + ', '.join[fruits]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Các chuỗi và số Concatenate: print mystr + ', '.join[fruits]
9, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
0 toán tử, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
3, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
4, F-String
print mystr + ', '.join[fruits]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Nhà điều hành
print mystr + ', '.join[fruits]
9s = 'aaa' + 'bbb' + 'ccc'
print[s]
# aaabbbccc
s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'
s = s1 + s2 + s3
print[s]
# aaabbbccc
s = s1 + s2 + s3 + 'ddd'
print[s]
# aaabbbcccddd
Bạn có thể kết hợp các chuỗi chữ [i like the following fruits: banana, apple, plum, pineapple, cherry
1 hoặc i like the following fruits: banana, apple, plum, pineapple, cherry
2] và các biến chuỗi với toán tử print mystr + ', '.join[fruits]
9.
i like the following fruits: banana, apple, plum, pineapple, cherry
i like the following fruits: banana, apple, plum, pineapple, cherry
print mystr + ', '.join[fruits]
Nhà điều hành
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
0'i like the following fruits: banana, apple, plum'
0Bạn có thể nối một chuỗi khác vào một chuỗi với toán tử tại chỗ,
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
0. Chuỗi bên phải được nối sau khi biến chuỗi ở bên trái.'i like the following fruits: banana, apple, plum'
1Nếu bạn muốn thêm một chuỗi vào cuối biến chuỗi, hãy sử dụng toán tử fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
0.
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Concatenate bằng cách viết chuỗi chữ liên tiếp
'i like the following fruits: banana, apple, plum'
2Nếu bạn viết chuỗi chữ liên tiếp, chúng được nối.
'i like the following fruits: banana, apple, plum'
3Ngay cả khi có nhiều không gian hoặc dòng mới với dấu gạch chéo ngược
i like the following fruits: banana, apple, plum, pineapple, cherry
7 [được coi là các dòng tiếp tục] giữa các chuỗi, chúng vẫn được nối.- Viết một chuỗi dài trên nhiều dòng trong Python
Bạn không thể làm điều này cho các biến chuỗi.
'i like the following fruits: banana, apple, plum'
4Các chuỗi và số Concatenate: print mystr + ', '.join[fruits]
9, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
0 toán tử, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
3, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
4, F-String
print mystr + ', '.join[fruits]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Hoạt động
print mystr + ', '.join[fruits]
9 giữa các loại khác nhau làm tăng lỗi.'i like the following fruits: banana, apple, plum'
5Nếu bạn muốn kết hợp một chuỗi và một số, chẳng hạn như số nguyên
result = "i like the following fruits: "+', '.join[fruits]
3 hoặc điểm nổi result = "i like the following fruits: "+', '.join[fruits]
4, hãy chuyển đổi số thành một chuỗi bằng fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
3 và sau đó sử dụng toán tử print mystr + ', '.join[fruits]
9 hoặc toán tử fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
0.'i like the following fruits: banana, apple, plum'
6Sử dụng hàm
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
4 hoặc phương thức chuỗi fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
4 Nếu bạn muốn chuyển đổi định dạng số, chẳng hạn như đệm không hoặc số thập phân.- Chuỗi - Đặc điểm kỹ thuật của ngôn ngữ Mini - Python 3.8.1 Tài liệu
'i like the following fruits: banana, apple, plum'
7Tất nhiên, cũng có thể nhúng giá trị của một biến trực tiếp trong một chuỗi mà không chỉ định định dạng, đơn giản hơn so với sử dụng toán tử
print mystr + ', '.join[fruits]
9.'i like the following fruits: banana, apple, plum'
8Trong Python 3.6 trở lên, bạn cũng có thể sử dụng một chuỗi theo nghĩa đen [chuỗi F] được định dạng. Nó thậm chí còn đơn giản hơn
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
4.- 2. Phân tích từ vựng - Chuỗi được định dạng theo nghĩa đen - Python 3.9.4 Tài liệu
'i like the following fruits: banana, apple, plum'
9Concatenate một danh sách các chuỗi thành một chuỗi: fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Bạn có thể kết hợp một danh sách các chuỗi vào một chuỗi với phương thức chuỗi,
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5.- Các loại tích hợp - str - tham gia [] - Python 3.8.1 Tài liệu
Gọi phương thức
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5 từ ', '.join[str[f] for f in fruits]
5 và vượt qua ', '.join[str[f] for f in fruits]
6.print mystr + ', '.join[fruits]
0Nếu bạn sử dụng một chuỗi trống
', '.join[str[f] for f in fruits]
7, ', '.join[str[f] for f in fruits]
6 chỉ đơn giản là được nối và nếu bạn sử dụng dấu phẩy ', '.join[str[f] for f in fruits]
9, thì nó sẽ tạo ra một chuỗi phân loại bằng dấu phẩy. Nếu một ký tự mới s = s + ', '.join[[str[fruit] for fruit in fruits]]
0 được sử dụng, một dòng mới sẽ được chèn cho mỗi chuỗi.print mystr + ', '.join[fruits]
1Lưu ý rằng các đối tượng có thể đi được khác như Tuples có thể được chỉ định là đối số của
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5.Sử dụng
s = s + ', '.join[[str[fruit] for fruit in fruits]]
2 để phân chia một chuỗi được phân tách bằng một dấu phân cách cụ thể và lấy nó làm danh sách. Xem bài viết sau đây để biết chi tiết.- Chia chuỗi trong Python [Delimiter, Line Break, Regex, v.v.]
Concatenat một danh sách các số vào một chuỗi: fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5, fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
3
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
Nếu bạn đặt một danh sách không chuỗi thành
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5, một lỗi sẽ được nêu ra.print mystr + ', '.join[fruits]
2Nếu bạn muốn kết hợp danh sách các số [
result = "i like the following fruits: "+', '.join[fruits]
3 hoặc result = "i like the following fruits: "+', '.join[fruits]
4] vào một chuỗi, hãy áp dụng hàm fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
3 cho mỗi phần tử trong danh sách hiểu để chuyển đổi số thành chuỗi, sau đó kết nối chúng với fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5.print mystr + ', '.join[fruits]
3Nó có thể được viết dưới dạng biểu thức máy phát, một phiên bản trình tạo của toàn bộ danh sách. Các biểu thức của máy phát được đặt trong ngoặc đơn
print[mystr, fruits]
0, nhưng bạn có thể bỏ qua print[mystr, fruits]
0 nếu biểu thức trình tạo là đối số duy nhất của hàm hoặc phương thức.print mystr + ', '.join[fruits]
4Nói chung, các biểu thức của máy phát có lợi thế là giảm sử dụng bộ nhớ so với sự hiểu biết danh sách. Tuy nhiên, vì
fruits = ['banana', 'apple', 'plum', 'pineapple', 'cherry']
mystr = 'i like the following fruits: '
print [mystr + ', '.join[fruits]]
5 chuyển đổi nội bộ chuyển đổi một trình tạo thành một danh sách, không có lợi thế nào để sử dụng các biểu thức của máy phát.- Python - Danh sách so với tốc độ hiểu biết của máy phát với chức năng tham gia - Stack Overflow
Xem bài viết sau đây để biết chi tiết về sự hiểu biết danh sách và biểu thức máy phát.
- Liệt kê sự hiểu biết trong Python