How to replace space in a string with \n

Terry Reedy tjreedy at udel.edu
Thu Jan 31 13:25:11 EST 2019


On 1/31/2019 11:19 AM, Ian Clark wrote:
> text = "The best day of my life!"
> output = ''
>> for i in text:
> if i == ' ':
> output +='\n'
> else:
> output += i
>> print(output)
>> throwing my hat in the ring, not only is it .replace free it is entirely
> method free

But this is an awful, O(n*n) way to solve an inherently O(n) problem, 
which I think should not be taught to beginners unless they are 
simultaneously taught to never do this for indefinitely large n.
-- 
Terry Jan Reedy


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /