How to ask for user input in Python – with example

  • Reading time:17 mins read

How to ask for user input in Python?

User input is an important part of any program regardless of the technology used. 

Programs often require input from the users in order to complete certain operations such as login and registration. In fully developed applications, dialogue boxes are the conventional way of soliciting user input.

 

 

Depending on the version, Python offers two conventional ways of accepting user input.

These functions are: 

                   –input() function in Python 3

                   –raw-input() in Python 2.7

In Python 3 besides allowing the user to enter input, this function also evaluates the input from the user.

Handling exceptions

Depending on the requirements of the program the function can either accept the input or raise an exception.

 

ask for user input in Python

 

It is also important to note that the input function stops the execution of any program until the user has entered the required input.

Regardless of the input that is entered the input function will always convert the input into a string. The input function can be implemented as shown below.

 

How to ask for user input in Python by printing out a string

While the above input function is simple and does not issue any guidelines to the user it allows the user to enter some kind of input. In this case, we have entered the string ‘cake’ as the input.

We can also provide some guidelines to enable the user to understand the kind of information that is most desired as shown in the code snippet below.

 

 

Besides the description, we have also used the f string method to embed the user input that is stored in the variable name ‘value’.

Using the print function we have then printed the string alongside the user input.

 

ask for user input in Python

 

From the description, it is evident that the user is required to enter any kind of food as the input.

How to ask for user input in Python – Using the type() function

As mentioned before the input function will always convert the user input to string, we can determine this using the type() function as shown below.

 

 

If we change the user input to a number it is certain that the input()  function would still convert the number into a string.

 

 

If our intention is to solicit user input in the form of a number then in that case we won’t be able to do any addition or arithmetic operation. However, Python provides a solution to this.

How to ask for user input in Python with typecasting

Using built-in functions such as int(), str() we can then perform typecasting to convert the user input to any type that we desire. Typecasting is the process of converting one literal type to another.

 

 

From the output, it is evident that we have successfully converted the user input from the string type to the integer type. We can now perform arithmetic operations using the user input.

In the code snippet below we are asking the user to enter their year of birth and the program will automatically calculate their current age. Using the f string we have embedded the calculation alongside a message. 

 

 

In such a program the user is strictly required to enter an integer as the input. An attempt to enter a string as the input will raise a ValueError. Let’s try to type in the word “CodeBerry”.

 

 

How to ask for user input in Python within a try-except block

This error may not be the best response that a user would expect, however using exception handling we could validate the user input and return a more readable message.

 

ask for user input in Python

 

In this case, we will let the user know that only numbers are considered as valid input. Let’s type the word ‘CodeBerry’ in the terminal:

 

 

How to ask for user input in Python by accepting multiple inputs

There are instances where we may require the user to enter multiple inputs. A typical example of this would be asking the user to enter their first name and last name.

There are different approaches to this and one of these approaches would be duplicating the input function every time we want a different output.

 

How to ask for user input in Python – Using the split() function

However, a more efficient way will be to use the split() function. In Python, the split function splits a string into a list based on the specified separator.

In this case, we will use whitespace as the separator to create a list of two arguments that the user will enter simultaneously. 

Now using the f string method we will then pass an expression calculating the current age of the user and display the name alongside the string.

 

 

In order to successfully compute the current age of the user, we have used the int() function to convert the user’s age into an integer.

Failure to do so may result in an error since the input function always converts the user input into a string.

 

Summary

This is how to ask for user input in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.