ใช้オペอเรเตอร์ + (plus) เพื่อ Concatenate (ต่อ) สตริงสองสตริงเข้าด้วยกัน
ตัวอย่าง
>> s1 = 'TutorialsPoint ' >>> s2 = 'Hyderabad' >>> print(s1 + s2) TutorialsPoint Hyderabad
การต่อหลายสตริง
>> 'a' + 'b' + 'c' + 'd' 'abcd' >>> # ใช้ join() แทน (เร็วกว่า) >>> ''.join(['a', 'b', 'c', 'd']) 'abcd'
ข้อจำกัดของ +
- ต่อได้เฉพาะ String กับ String เท่านั้น
'hello' + 123→TypeError- ต้องแปลงก่อน:
'hello' + str(123)
ประสิทธิภาพ: + vs join()
แนะนำ: ใช้
join()สำหรับต่อสตริงหลายตัว ใช้+แค่ 2-3 ตัวเท่านั้น