Created
January 24, 2020 18:07
-
-
Save tinjaw/9e0ecee978d1a42c1c0d0d4e2c1a99b7 to your computer and use it in GitHub Desktop.
Saved from https://towardsdatascience.com/10-python-tips-and-tricks-you-should-learn-today-a05c23a39dc5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When you need to concatenate a list of strings, you can do this using a for loop by adding each element one by one. However, this would be very inefficient, especially if the list is long. In Python, strings are immutable, and thus the left and right strings would have to be copied into the new string for every pair of concatenation. | |
A better approach is to use the join() function as shown below: | |
```python | |
characters = ['p', 'y', 't', 'h', 'o', 'n'] | |
word = "".join(characters) | |
print(word) # python | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment