Displaying text 2

In the last lesson, we looked at simple methods to display text in Python. In this lesson it does not get any harder. I simply want to show you 2 new skills that you may find useful.

Escape codes

In Displaying text lesson 1, I showed you how to display text that included double quotes. Another way to solve hte same problem would be to use escape codes that ignore the fact that there are double quotes that may end the string.

Also click here for more examples of python Escape characters / sequences

Click run on the Python editor below to see that both the lines of code below produce exactly the same result.

Escape codes (or also known as escape sequences) can be used for many purposes in Python. One other very useful example can be add a new line when displaying text.

Displaying text on multiple lines

Leading on from the last tip showing escape codes, it is also very useful to display text over multiple lines by using a multi line print function. You can print on multiple lines in python by writing the print function like so:

print('''
blah blah blah
blah blah blah
blah blah blah
''')

The string within the print function must use 3 quote characters and also end with 3 quote characters. Try this on the Python editor below.

Here is an example of printing an ASCII image using multi line print for a text based game in Python.

Subscribe to LearnICT.it via Email

Apparently, this site uses cookies? As the web developer, I can't say that I want, need them or programmed my site with your cookies in mind... however, if you continue to use this site I will assume that you are happy with it.I love cookies!