Python: Printing with .format()

Another way to print variable values in Python is the .format(). This method replaces %d,%s, and %r, but I still showed them to you in an earlier lesson as you will still see them in use, I wanted you to know about them.

When using .format() – {} represents your place holder in the string.

pythonFormat.jpg

You can also use numbers to tell format which order to use values in.

pythonFormat1.jpg

You can name your place holders:

pythonFormat2

You can use user input

pythonFormat3.jpg

And you can even use dictionaries

pythonFormat4

Advertisements