How to replace space in a string with \n

Avi Gross avigross at verizon.net
Thu Jan 31 13:10:36 EST 2019


It is amazing to watch what happens when a fairly simple question is asked
to see how people answer.

In an effort not to ramble, I will summarize my thoughts. The student wanted
to know how to solve a problem using only what they already should know and
that specifically they should not use a method. In effect, they are being
asked to write the kind of code that might be used to make such a method.

There were many answers including some that absurdly suggested even more
advanced methods they should not know. Frankly, the question belongs more in
the sister list for tutoring and even there, the goal is NOT to supply an
answer but point out flaws in code provided as an attempt or suggest an
outline of a method and let the student fill it out and learn.

This is not only a public forum but one that is searchable indefinitely into
the future. Providing a full-blown answer not only hands a solution to one
student but to their classmates and any future takers.

I note another poster asking questions turns out to be not a student but
someone quite advanced who likes to learn just in time as they search for
what is needed. They require a very different approach and can learn well
from being handed a more detailed solution they can interpolate into their
project. 

So, how do you replace? I think a loop answer is probably best for this
student. For the second type of questioner, many of the others would be
great including suggesting they just use replace as there is nothing wrong
with that!

NOTE: It is great when we have an exact set of requirements. I note that the
requirement here might not require making a new string at all. If you just
need to PRINT the words on multiple lines, one solution is to call "print"
on each character in the loop using the appropriate method to suppress
printing a newline except when you see a blank.





More information about the Python-list mailing list