Input and type conversion
So far every value was written inside the program. Real programs ask the person using them. The input function shows a question, waits for the person to type and press Enter, and hands back what they typed.
Asking a question
name = input("What is your name? ")
print("Hello,", name)Output
What is your name? Asha
Hello, AshaIn the output above, the person typed Asha after the question and pressed Enter. The program then carried on with the next line.
Put a space at the end of the question text. Without it, the answer sits right against the question mark.
Input always gives text
This is the most important fact in the lesson. Whatever the person types, input returns a string, even if they typed digits.
age = input("Your age? ")
print(type(age))
print(age * 2)Output
Your age? 14
<class 'str'>
1414The person typed 14. Python has the text "14", not the number 14, so multiplying by 2 repeats the text and prints 1414. To do maths you must convert it.
Converting types
| Function | Turns the value into | Example | Result |
|---|---|---|---|
int() | whole number | int("14") | 14 |
float() | decimal number | float("3.5") | 3.5 |
str() | text | str(14) | "14" |
age = int(input("Your age? "))
print("Next year you will be", age + 1)Output
Your age? 14
Next year you will be 15Here input runs first and returns "14". Then int turns it into the number 14. Wrapping one function inside another like this is very common. Read it from the inside out.
Use float when the answer may have a decimal point, such as a price or a weight.
rate = float(input("Rate per kg? "))
kg = float(input("How many kg? "))
print("Amount:", rate * kg)Output
Rate per kg? 62.5
How many kg? 4
Amount: 250.0When conversion fails
If the text is not a proper number, the conversion stops with a ValueError.
number = int("twelve")What Python says
Traceback (most recent call last):
File "example.py", line 1, in <module>
number = int("twelve")
ValueError: invalid literal for int() with base 10: 'twelve'The same happens with int("3.5"), because of the decimal point. Convert with float first if you need to accept decimals. The lesson on errors and try/except shows how to catch this and ask again. There is also a page on ValueError.
Converting between number types
int on a float drops everything after the decimal point. It does not round.
print(int(7.9))
print(int(-7.9))
print(float(5))
print(round(7.9))Output
7
-7
5.0
8Use round when you want the nearest whole number.
Building a sentence from numbers
Numbers must become text before you join them with plus. An f-string does this for you and is easier to read.
marks = 432
total = 500
percent = marks / total * 100
print("Percentage: " + str(percent) + "%")
print(f"Percentage: {percent:.1f}%")Output
Percentage: 86.4%
Percentage: 86.4%A complete small program
This program asks for two things and gives a useful answer.
name = input("Name: ")
year = int(input("Year of birth: "))
age = 2026 - year
print(f"{name} turns {age} in 2026.")Output
Name: Ravi
Year of birth: 2010
Ravi turns 16 in 2026.Common mistakes
- Forgetting to convert:
input("Age? ") + 1is an error, because text and a number cannot be added. - Converting too early:
int(input)without the brackets afterinputtries to convert the function itself. - Expecting
intto round. It cuts.
Try it yourself
In these answers the typed values are written into the program so that the result can be shown. In your own copy, replace them with input.
Exercise 1. Ask for the length and width of a room in feet and print its area. For the test, use 12 and 10.
Show one answer
length = float("12") # float(input("Length? "))
width = float("10") # float(input("Width? "))
print("Area:", length * width, "square feet")Output
Area: 120.0 square feetExercise 2. Ask for a temperature in Celsius and print it in Fahrenheit. The rule is multiply by 9, divide by 5, add 32. Test with 37.
Show one answer
celsius = float("37") # float(input("Celsius? "))
print(celsius * 9 / 5 + 32)Output
98.6Exercise 3. Ask for a bill amount and the number of friends, and print each person's share to two decimal places. Test with 1250 and 4.
Show one answer
bill = float("1250") # float(input("Bill? "))
friends = int("4") # int(input("Friends? "))
print(f"Each pays {bill / friends:.2f}")Output
Each pays 312.50Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.