by bjourne · 13y ago · 0 comments
s = ""
for x in range(100):
s += str(x)
print s
If s is a sequence of characters, then for each iteration of the loop s needs to be reallocated to encompass the appended str(x) characters.If s is an abstract string, then the += operation can be implemented as just adding a reference to the string to eventually be appended. Then when the print operation occurs, the whole string can be materialized in one go so that it can be printed.
Another, and more philosophical argument against string as a sequence of characters is that a string is no more a sequence of characters, than it is a sequence of symbols or a sequence of words or a sequence of sentences.
s = []
for x in range(100):
s = s.append(x)
format s
In other words, this is something that holds just the same for vectors of any type.The "philosophical" "argument" is really not relevant, since the difference between characters and "symbols" seems to be just what you want (whatever that is), while words and sentences can be represented by strings themselves. This is just a tangent that adds nothing to the question of how to represent a string.
Furthermore it isn't god given that the right way to iterate a text is character by character. Word by word or line by line is equally common. It's therefore completely arbitrary whether it's a character sequence, a line sequence or something else.