One of the most fundamental parts of programming is interacting with the user. How do you ask the user for information, and how do you display output back to them? In Python, this is made simple with two built-in functions: input()
for getting user input and print()
for displaying output.
🎤 Getting User Input with the input() Function
To get input from a user, you use the input()
function. When your program calls this function, it pauses and waits for the user to type something and press the Enter key. Whatever the user types is returned by the function as a string (a sequence of characters). You can also provide an optional prompt inside the parentheses to tell the user what to enter, like this: name = input("Enter your name: ")
.
🔢 Converting Input to Numbers
A crucial thing to remember is that input()
always gives you a string. If you need a number to perform calculations, you must convert it. You can convert a string to an integer using the int()
function or to a floating-point number (a number with a decimal) using the float()
function. For example: age = int(input("How old are you? "))
will convert the user’s input into a number that you can use for math.
📺 Displaying Output with the print() Function
To show information to the user, you use the print()
function. You can give it a single value to display, like print("Hello!")
, or even a variable, like print(name)
. If you want to display multiple items on the same line, you can separate them with commas. Python will automatically add a space between them for you. For example: print("Your age is", age)
.
These two functions are the building blocks of any interactive program. By combining them, you can create scripts that feel dynamic and responsive to the user.
—
Stephenson, Ben. The Python Workbook: A Brief Introduction with Exercises and Solutions. 3rd ed., Springer, 2025.
More Topics
- How to Pass Command Line Arguments to Your Python Script
- Solving the Classic FizzBuzz Problem in Python: A Step-by-Step Tutorial
- How to Solve the Pythagorean Theorem with a Python Script
- What’s the Difference Between Syntax, Runtime, and Logic Errors in Python?
- What Are Dictionaries in Python and How Do You Use Them?
- Thinking Recursively: A Beginner’s Introduction to Recursion in Python
- How to Read and Write Files in Python