Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Nối string không dùng toán tử + trong Python

Dùng str.join(list_of_strings) từ class string. String gọi method dùng để join list string truyền làm argument.

Ví dụ

>>> ' '.join(['Hello', 'World'])
'Hello World'
>>> ''.join(['Hello', 'World', 'People'])
'HelloWorldPeople'
>>> ', '.join(['Hello', 'World', 'People'])
'Hello, World, People'

Lưu ý: string gọi method được chèn giữa các string trong list argument.