Input and Casting

Input function

The input() function reads text typed by the user. You can use it in two ways. For example, to ask for the user's name, you can do this:

Or you can do this:

With the first way, the user types on the line after the prompt. With the second way, the user types on the same line as the prompt.

User input is always a string. To get a number, you need to cast the string to an int or a float. Casting means converting a value from one type to another.

Each of the first two lines above combines two functions. First input() gets a string from the user. Next int() or float() casts that string to a number. That number is then assigned to the variable. We can also call those functions on separate lines:

If the user enters text that cannot be cast to the requested type, Python reports an error. For example, what happens if you enter "hi" for the height in meters in the earlier example? Try it.

Here is an example that asks the user for two numbers and prints out their sum:

You can also cast an int to a float:

Or a float to an int, which truncates everything after the decimal point:

Check your understanding. Predict the result. One value is cast to a number and one stays a string.

typed: str = "5"
x: int = int(typed) + 1
y: str = typed + "1"

Predict what each variable holds after the program finishes. The program is not run for you. Replace each None in the box below with your prediction, and give each variable a type hint, like name: type = value. Then press Run to find out if you are right. Write text in quotes, like "abc".

  • final_x is the value of x at the end
  • final_y is the value of y at the end
Hint 1

int() makes a number, so + adds. A string stays a string, so + joins.

You can also cast values to strings with str().

Check your understanding. Predict the result. Check what int() does to a decimal, and what str() gives back.

n: int = 42
x: int = int(9.99) + 1
y: str = str(n) + "0"

Predict what each variable holds after the program finishes. The program is not run for you. Replace each None in the box below with your prediction, and give each variable a type hint, like name: type = value. Then press Run to find out if you are right. Write text in quotes, like "abc".

  • final_x is the value of x at the end
  • final_y is the value of y at the end
Hint 1

int() truncates. Adding a string to a string joins them.

Multiplying a string

A string times a whole number repeats the string. This is the other half of the difference between a string and a number: * on numbers multiplies, and * on a string repeats.

The second line prints 555, because "5" is text. The third line prints 15, because int() turned the text into a number first.

Check your understanding. Predict the result. The text "4" is a string until int() changes it.

typed: str = "4"
a: str = typed * 2
b: int = int(typed) * 2

Predict what each variable holds after the program finishes. The program is not run for you. Replace each None in the box below with your prediction, and give each variable a type hint, like name: type = value. Then press Run to find out if you are right. Write text in quotes, like "abc".

  • final_a is the value of a at the end
  • final_b is the value of b at the end
Hint 1

* repeats a string and multiplies numbers. Ask which one typed is, and which one int(typed) is.

Recall: click each blank to check yourself

Fill in the operator for each comment.

remainder: int = 7  2  # 1
whole: int = 7  2  # 3
exact: float = 7  2  # 3.5
power: int = 2  3  # 8
total: int = 10
total  5  # 15
Explanation

/ always gives a float. // rounds down to a whole number.

Recall: click each blank to check yourself

input() always gives back a . Fill in the casting functions.

text: str = input("Age: ")
age: int = (text)
height: float = (input("Height: "))
label: str = (age)
Explanation

Cast the text before doing math on it.

Quizzes

The quizzes below are written as functions you can test without typing input. In a real program you would read these values with input(), but a function that takes its values as parameters is easier to test and reuse.

  1. Write a function named greet that takes a name parameter and prints out "Hello" followed by the name. For example, greet("Sam") should print Hello Sam.
  1. Write a function named multiply that takes two numbers and prints out "The result is" followed by the result of multiplying those numbers. For example, multiply(3, 4) should print The result is 12.
  1. Write a function named divide_report that takes two integers, a dividend and a divisor, and prints a report in this exact form:

    <divisor> goes into <dividend> <i> times with <r> as the remainder.

where <i> is the whole number of times the divisor goes into the dividend and <r> is the remainder. For example, divide_report(17, 5) should print:

`5 goes into 17 3 times with 2 as the remainder.`
Hint 1

Two of the arithmetic operators from earlier in this lesson answer these questions: how many whole times does it fit, and what is left over?

Projects

Practice what you learned in your own editor. Each project has its own page.