Python Forum
forming a str one by one - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: forming a str one by one (/thread-23962.html)



forming a str one by one - Skaperen - Jan-24-2020

some complicated code needs to form a str one character at a time, as it figures out each character, one at a time. these characters can be any Unicode character, so inserting or appending bytearray is not an option. maybe forming a list is an option. but, i need to end up with a str type result. appending to a growing str type is not an option since str is immutable. i could reconstruct a new str each time like newstr = oldstr + character, but that would be a lot of work. or would it? is there a better way to build a str one character at a time?


RE: forming a str one by one - buran - Jan-24-2020

a list and pass it to ''.join() when you need the str?
what produce/yield the individual chars that will form the str? can it be a generator that you can pass to ''.join(), removing the need for intermediate list?


RE: forming a str one by one - Skaperen - Jan-25-2020

it probably is possible to make it be a generator. that sounds like a very good idea.