Created
March 22, 2012 20:48
-
-
Save beaumartinez/2164188 to your computer and use it in GitHub Desktop.
Concatting strings
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
# 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