Skip to content

Instantly share code, notes, and snippets.

@beaumartinez
Created March 22, 2012 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beaumartinez/2164188 to your computer and use it in GitHub Desktop.
Save beaumartinez/2164188 to your computer and use it in GitHub Desktop.
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