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

PythonServer Side ProgrammingProgramming

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.


raja
Published on 14-Dec-2017 14:21:43
Advertisements