Thomas Kelly

Python's Print & Format Functions

20 Oct 2018 tarihinde yayınlandı.

print_intro

Being able to print out your results in Python in a readable and understandable manner is quintessential to being able to interpret your code in a meaningful way. When coding, it’s always a best practice to occasionally stop and print your results to make sure the code is working as intended.

In order to better achieve this, using the .format function is invaluable.

It’s generally written at the end of a print as such:

print("Hello {}!".format('Tom'))

Will give us the result of: print1

You can also use it to both calculate and print simple outputs, as such:

x = 2
y = 3
print("You have successfully added {} to {} and printed {}!".format(x, y, x+y))

Gives us an output of: print2

Alternatively, you can use ‘f’ before the string to simplify formatting, and triple quotes at the beginning and end to print multiple lines. We even can take this a step further and input functions from other parts of Python to have it print the results. Using data from a previous SAT participation project I worked on:

print3

Print and format are two of the most important functions to know in order to become a good Python programmer!