Talking to the User
Beginner · 18 min read · ▶ live playground · ✦ checkpoint
Right now your programs can calculate, but they mostly talk to themselves. The next step is small and powerful: show results in a human shape, ask a person for a value, and turn that typed text into data Python can use.
print() is more flexible than it looks
You have used print() to write text to the screen. It can also print several values at once:
player = "Mina"
score = 42
mood = "focused"
print(player, score, mood)
print("Player:", player)
print("Score:", score)
print(player, score, sep=" | ")
print("Loading", end="...")
print("done")Mina 42 focusedPlayer: MinaScore: 42Mina | 42Loading...done
By default, print() puts one space between values and moves to a new line at the end. Those defaults are controlled by parameters, named settings a function understands; functions get their full story in lesson 7.1.
The sep parameter controls the separator between printed values. The end parameter controls what print() writes after the last value.
Read sep=" | " as "put this between the values." Read end="..." as "finish this print with dots instead of a new line." You won't use these on every line, but they make output feel deliberate instead of dumped.
input() always gives you text
Input is data that comes into a program from outside it. Here, the outside world is a person typing into the playground.
Python's input() function pauses the program, shows a prompt, a short message asking the user for something, and returns what the user typed as a string.
player = input("Player name: ")
print("Hello,", player)
print(type(player))Player name: 7Hello, 7<class 'str'>
In that run, the person typed 7 at the prompt — and Python still treated it as a str. That is the key rule: input() always returns text. If a person types digits, Python still sees quoted-digits-style data until you convert it.
Convert text when you need numbers
Type conversion, often called casting, means making a new value of one type from a value of another type. The original input text stays text; Python creates a new object for the converted value, matching the sticky-label model from lesson 2.1.
Use int(...) for whole numbers, float(...) for decimal-style numbers, and str(...) when you need text:
apples_text = input("Apples collected: ")
apples = int(apples_text)
bonus = 3
print(apples + bonus)
print(type(apples_text))
print(type(apples))
print(str(apples))Apples collected: 710<class 'str'><class 'int'>7
The first line asks for text. The second line converts that text to an int. That conversion is what lets apples + bonus run as numeric addition; without it, Python would be trying to add a str and an int. Section 3 will go deep on strings; today, the main habit is clear: convert before doing number work.
float(...) works the same way when a value may contain a decimal point.
bool(...) is different. For the values shown here, it returns False for zero or empty text and True for present text or nonzero numbers. An empty string is text with zero characters. This yes/no rule is truthiness, Python's way of treating values as true or false; the full story comes in lesson 4.1.
ticket_text = "7.5"
ticket_price = float(ticket_text)
print(ticket_price)
print(type(ticket_price))
print(bool(0))
print(bool(1))
print(bool(""))
print(bool("False"))7.5<class 'float'>FalseTrueFalseTrue
That last line is weird on purpose. The string "False" is not empty text, so bool("False") is True. Do not use bool() to understand a person's typed yes-or-no answer yet. Save that for conditional logic in Section 4.
Try it — a tiny interactive receipt
Run this, then answer the prompts. Use a name for the first answer and digits for the second answer.
f-strings make messages readable
An f-string is a string with an f before the opening quote; it lets you place expressions inside {} and have Python insert their values.
Without f-strings, a message with several pieces can get noisy:
player = "Mina"
apples = 7
bonus = 3
total = apples + bonus
print(player, "gets", total, "apples after the bonus.")
print(f"{player} gets {total} apples after the bonus.")
print(f"Next target: {total + 5} apples")Mina gets 10 apples after the bonus.Mina gets 10 apples after the bonus.Next target: 15 apples
The first two lines print the same message. The f-string version keeps the sentence in one place, so it reads like the output you want. The braces hold expressions, and Python replaces each one with its value.
Comments are notes for humans
A comment is text in your code that Python ignores. In Python, a comment starts with # and runs to the end of the line.
Comments should explain why a line exists, not repeat what the line obviously says:
player = "Mina"
bonus = 3
score_text = "7"
# Convert before arithmetic, because typed input starts as text.
score = int(score_text)
print(f"{player}: {score + bonus}")Mina: 10
Good comments are notes to a future reader. Sometimes that reader is a teammate. Often it is you, two weeks later, wondering what you were thinking.
Checkpoint
Answer all three to mark this lesson complete
You can now make a small program speak, listen, and shape its answer for a person. Section 3 zooms in on the text side of that conversation, because strings have far more useful moves than quotes alone suggest.