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.

print(' My name is...\nMy name is...\nMy name is... \n \n Slim Shady')

Click run on the Python editor below to see how this may work.

 

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.

Python ascii doors

Posted in Uncategorized.

Leave a Reply

Your email address will not be published. Required fields are marked *

*