user input output in python


You wish to create a Python program that runs in the console and has some input/ output interaction with the user. For example, it’s asking some input data to be given like name, age, etc. In this post we will see how to accomplish that.


In Python, we have the built-in function input() that allows us to prompt the user to enter a value in the console. We can store that value in a variable and manipulate it. By default the input() function returns a string.

Let’s see an example:
					name = input("What is your name: ")
print("Hello " + name)


The code above prompts the user to enter their name, it’s storing the string value in the name variable and then it prints a message using string concatenation.

We can see the type of the name variable by using the built-in function type() as follows:
					print(type(name)) #returns the data-type of the variable
type in python

The name is of type class str (string)

Reading numbers from user

What happens if we wish to prompt the user to enter their age that is integer? In this case we can attempt to convert the input to an integer as follows:
					age = int(input("What is your age: ")) #convert to int


The int() function will attempt to convert the string value to integer. In this case if I enter the value 25, the input will return “25” as a string. The int() function will convert it to numerical 25 so mathematical operators can be applied. 

For example we wish to calculate the year the user was born. We simply subtract the age from the current year.
					current_year = 2050
year_born = current_year - age
print("You were born in " + year_born)

The code above it’s calculating the year the user was born and then printing it nicely.


What happens if the user enters a value that cannot be converted to integer? For example when the following command executes:
					age = int(input("What is your age: "))


The user enters “20 years old”. Then the program will crash with the following error:

valueError python example

This is a ValueError, it cannot convert it to base 10 (digits)

Reading a floating point arithmetic value (float)

We wish to prompt the user for a price. In this case, we can use the built-in float() function that accepts a string parameter and attempts to convert it to its respective floating point value. 

In combination with the input() function it will look as follows:
					price = float(input("Enter price of burger: "))
print("The burger costs " + price)

Similarly with the int() function, the code above attempts to convert the string value entered by the user into a float. If the value is not convertible then the program crashes with the same type of error as before, i.e. ValueError.

ValueError python example float fail


In this post we saw how to use the input() function to read data from the user. Moreover, we saw how to convert that input to integer and float by using the int() and float() functions respectively.

Share it!

Picture of Ellion


Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x