Computer >> Computer tutorials >  >> Programming >> Python

What is the preferred way to concatenate a string in Python?


The best way of appending a string to a string variable is to use + or +=. This is because it's readable and fast. They are also just as fast. Other than this, if you're working with 2 strings, append() can also be used.

If you are concatinating a list of strings, then the preferred way is to use join() as it accepts a lis tof strings and concatenates them and is most readable in this case.

If you are looking for performance, append/join is marginally faster there if you are using extremely long strings. But if you're using exteremly long strings, you're doing something wrong.