When learning basic Python, I had to restructure the way I looked at things. An early problem I encountered was the simple task of printing output to new lines, one after the other.
As I found out, it wasn’t as easy as pressing the ‘Enter’ key on my keyboard and hoping for the best.
Example
Let’s say I want to print the following sentence:
The quick brown fox jumps over the lazy dog.
In Python, I would do it as such:
print('The quick brown fox jumps over the lazy dog')
That’s fine as it is, but if I wanted to make sure Python printed the sentence like this, how would I go about it?
The quick brown fox
jumps over
the lazy dog.
In Python, one way to make a new line for a String is to use the line break utility ‘\n‘. Note that we must use ‘\n’ as a String data type itself.
So to output the sentence as we want it, our code will now look like:
print(
'The quick brown fox'
'\n' 'jumps over'
'\n' 'the lazy dog'
)
This code returns exactly the output we want.
>>>
The quick brown fox
jumps over
the lazy dog
>>>
I hope this has helped.