Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Join สอง String ให้กลายเป็น String เดียวใน Python

1. ใช้ concatenation operator +

str1 = "Hello"
str2 = "World"
str3 = str1 + str2
print(str3)  # HelloWorld

2. ใช้ str.join(seq) รวมหลายสตริงพร้อม separator

s = "-"
seq = ("a", "b", "c")
print(s.join(seq))  # a-b-c

สตริง s จะกลายเป็น separator ระหว่างแต่ละ element