HTuse HTuse
HTuse Python ·
10 steps

How to Concatenate Strings in Python?

Step 1

Use the `+` operator: `s3 = s1 + s2`

Step 2

Use `+=` for appending: `s1 += s2`

Step 3

Use `join()` for multiple strings: `''.join([s1, s2, s3])`

Step 4

Use f-strings: `f"{s1}{s2}"`

Step 5

Use `format()`: `"{}{}".format(s1, s2)`

Step 6

Use `%` formatting: `"%s%s" % (s1, s2)`

Step 7

Use `join()` with a separator: `' '.join(words)`

Step 8

Use `str.join()` on an iterable: `separator.join(iterable)`

Step 9

Use `map(str, items)` before joining non-strings: `''.join(map(str, items))`

Step 10

Use `pathlib` or `os.path.join()` for file paths, not string `+` concatenation