Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Concatting strings
# The "idiomaitc" way to concat strings in Python is
characters = ('a', 'b', 'c')
string = ''.join(characters)
# What's nice is that we can store all the strings in a list
characters = list()
for codepoint in range(ord('a'), ord('z') + 1):
character = chr(codepoint)
characters.append(character)
# And concatenate that
string = ''.join(characters)
# Which, especially for larger strings, ends up being more efficient--
characters = ''
for codepoint in range(ord('a'), ord('z') + 1):
character = chr(codepoint)
characters += character
# This way, we create a *new* string *every iteration* of the loop. If it's 1,000,000 characters long, you can imagine--that's quite an overhead!
# This is good read on the subject--http://www.skymind.com/~ocrow/python_string/
# :-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment