Talk to your program
Learn to receive data from the user with input() and work with strings.
What You'll Learn
- Use input() to read data from the keyboard
- Understand what strings are and how to combine them
- Convert text to numbers using int() and float()
- Use f-strings to format messages
Up until now, your programs have been a bit... lonely. You told them what to do, they listened, and that was it. But today everything changes. You will learn how to make your program ask questions and respond based on what the user says. Basically, you will build the first steps toward a chatbot.
Imagine a game where the computer asks your name, your age, and then tells you something personalized. Or a text adventure where you choose what happens next. It all starts with one simple function: input().
Up until now, the hero in the Python Kingdom had a fixed name and class. It is time to let the player choose! In this lesson you make character creation interactive using input(), display stats with f-strings, and build a shop where the player types what they want to buy.
The input() function
The input() function stops the program and waits for the user to type something from the keyboard. When the user presses Enter, the text they typed is returned as a result.
When you run this code, the program displays "What is your name? " and then waits. If you type "Alex" and press Enter, you will see:
What is your name? Alex
Hello, Alex
The text between quotes in input("...") is the message displayed to the user. It is like a question your chatbot asks. You can put any text you want there, or you can leave the parentheses empty if you do not want to display anything:
One very important thing: input() always returns a string (a sequence of characters). Even if the user types a number, Python treats it as text. We will see why this matters very soon.
Strings
A string is simply text. In Python, you create a string by putting text between quotes. You can use single or double quotes, both work identically:
You choose between single and double quotes based on what is more practical. For example, if your text contains an apostrophe, you use double quotes to avoid issues:
Or the other way around, if the text contains double quotes:
Strings can be empty (no characters) or contain very long texts:
String concatenation
Concatenation means joining two or more strings together. In Python, you do this with the + operator:
Notice that Python does not automatically add a space between them. If you want a space, you must add it yourself:
You can combine concatenation with input() to build personalized messages:
If the player types "Andrew" and "Mage", they will see:
Hero Andrew is a legendary Mage!
You can also multiply a string by a number to repeat it:
Type conversion
Here is where things get interesting. Remember: input() always returns a string. This means that if you ask the user how old they are, the answer is text, not a number.
Why does it matter? Because you cannot do math with text:
To transform a string into a number, you use:
int()- converts to an integer (no decimals)float()- converts to a decimal number
You can write everything on a single line by putting input() directly inside int():
Let's see a complete example where our chatbot does a calculation:
Notice that we also used str() at the end. Why? Because approx_age is a number and you cannot concatenate a number with a string. The str() function does the reverse conversion: it turns a number into text.
Common errors
When working with input() and conversions, you will encounter some classic errors. Let's look at them so you know how to fix them.
TypeError - mixing different types:
Python will tell you: TypeError: can only concatenate str (not "int") to str. The fix: convert with int() before doing math.
ValueError - text that cannot be a number:
Python will throw: ValueError: invalid literal for int() with base 10: 'hello'. This happens when the user types text instead of a number. For now, make sure you type a number when the program asks for one. In a future lesson you will learn how to handle this situation gracefully.
Surprise concatenation - numbers as strings:
This is one of the most common traps. Since input() returns strings, + joins them instead of adding them. The fix: convert both values to int() or float().
f-strings
Concatenation with + works, but it can get clunky when you have many variables. Python offers a much more elegant method: f-strings (formatted strings).
An f-string starts with the letter f before the quotes, and you put variables inside curly braces {}:
Result: Player Andrew has reached level 7!
Compare the two approaches:
In f-strings you can put expressions too, not just variables:
Let's combine everything we learned into a mini chatbot:
The \n character in the f-string creates a new line, making the text more airy and easier to read.
The input() function always returns a string. If you want to do math, convert with int() or float(). And for beautiful messages, f-strings are your best friend.
Exercise : The hero's name in the Python Kingdom
Exercise : Interactive class selection
Exercise : Hero Sheet with f-strings
Exercise : Interactive shop
Exercise : Temperature converter
Exercise : Tip calculator
Complete game code: Python Kingdom (Lesson 3)
Now your game has interactive character creation! The player chooses their name and class, sees stats with f-strings, and can buy from the shop:
Test Your Knowledge
Check how well you understood the lesson with these 5 questions.

