Skip to main content

User Output

This section covers the basics of how to display output for a user to see, provides coding examples, and reveals some useful tips to help you out when programming.

You should already be very familiar with the print function in Python. There are a few new details we can discuss to control output better. By default the print statment ends with a newline ("\n"), however you can control this by providing an end argument as shown below.

# Prints messages with differnt end arguments
print("First Statment", end="--")
print("Second Statment")
print("Third Statment", end="**")

Output Formatting

We can make out output very nice by utilizing the format function for Strings. Once key feature is to use {} as a place holder in text.

name = "Jonathan"
age = 21
print("My name is {} and I'm {} years old.".format(name,age))

In Python, floats can sometimes be VERY long, so another common use case is to control the number of decimals when printing a number. The example below shows how to display only 3 decimal places.

longNumber = 8/7
print("The number {.3f} has 3 decimal places.".format(longNumber))