We have seen one way of doing output so far, and that is:

print("Yup, this is some text!");

This is the primary way we’ll be handling output for this class.

But… where does print come from? What is it?

print in Python is what we call a built-in function. Whenever you create a program using Python, there’s a lot of complicated stuff under the hood that you don’t want to deal with! Notice that when your program runs, you get a nice little output window with some text in it. You didn’t need to specify WHERE the window would show up, or its color, or what font to use for the window, or anything else related to that! All you have to do is call the print function, and all that other stuff is handled for you!

The developers of Python realized that programmers don’t want to reinvent the wheel every time they write a program. Thus, Python has many built-in functions you can use to accomplish common or simple tasks. In fact, most other programming languages have similar built-in functions or starter-kits of common code. Printing is one of Pythons built-in functions, and we will encounter more throughout the semester.

A list of Python’s built-in functions can be found here: https://docs.python.org/3/library/functions.html

OK, so we have this print built-in function… now what?

Output text

(for showing text to the user of the program)

#Write text
print("Yeah, this is some text")

Output

Yeah, this is some text!

Ouput two lines of text

#Using the newline character
print("Some text\nand some more!")

Output

Some text and some more!

Output a variable, as text

#Write out the value of the variable x
x = 5
print(x)

Output

5

Output a variable inside other text

#Write a line of text with the value of x in it
x = 5
print(f"My number is {x}")

Output

My number is 5

Output multiple variables

y = 10
z = 82.44
print(f"Numbers:{y} and {z}")

Output

Numbers:10 and 82.44

Input

Getting user input

x = input()
print(x)

Output

(whatever the user originally entered will print out here)

Wait, what is the code above doing? We’re basically just getting some user input… and then immediately printing it back out to the screen! What else can we do with user input? And how can we go about using it in our calculations?

Getting user input with a prompt

x = input("Please enter something funny!")
print(x)

Output

Please enter something funny! (whatever the user originally entered will print out here)

Typecasting

Sometimes, we’d like to change a variable’s data from one type to another. Suppose we use the input utility to so a user can enter text input to our program. What if we’d like the user to enter an integer? The problem is, input only gives us a string to work with; it won’t give us data in the form of an integer or float.

To change our data from one type to another, we can use something called typecasting, and it works as follows:

x = input("Enter a whole number")
y = int(x)

You could also shorthand this another way, if you were definitely expecting an integer input:

x = int(input("Enter a whole number"))

So there may be situations, especially after using the input utility, where you need to change a string into another type.

Formatting

There may also be times when you want to format your output a particular way. For example, doing this division:

x = (1.0/3.0)

yields this, when printed: 0.3333333333333333

What if we only wanted a few decimal places, and not a bunch? We can format our output like so:

x = 1.0/3.0
print(f"{x:.2f}")
print(f"{x:.3f}")
print(f"{x:.4f}")
print(f"{x:.5f}")

Output

0.33
0.333
0.3333
0.33333

Oooh, fancy! Our print statement above looks a little wonky, but now it only prints out a specific number of decimal places! The number after the colon and period specifies the number of decimal places we’re printing out.