The input() function pauses your program, shows a prompt, and returns a string containing whatever the user typed.
To do arithmetic or other non-text work, convert that string to the needed type.
x = input("Enter a number: ") # x is a string
print(type(x)) # <class 'str'>
# Convert to int, add 1
y = int(x) + 1
print("x + 1 is", y)
# Other built-in converters
int("7") # -> 7
float("3.14") # -> 3.14
bool("") # -> False (empty string)
bool("hello") # -> True (any non-empty string)
str(42) # -> "42"Note: For booleans, an empty value (
"",0,0.0) isFalse. Anything else isTrue.
- Create a file called
Task_9.py. - Ask the user for their age with
input("Age: ")and store it inage_str. - Convert
age_strto an integer namedage_int. - Print a message using an f-string:
You will be <age_int + 5> in five years. - Ask the user to enter a decimal price, convert it to
float, add 20% tax, and print the total. - Finally, prompt the user for any text, convert it to
bool, and print whether Python considered itTrueorFalse.
Run the script several times with different inputs to see how each conversion behaves.