All Courses

User Input in Python

Shashank Shanu

3 years ago

Table of Content
  • User Input Function
  • Basic Example
       In this article, we will try to learn about the input function. Many times while working on a project you need to take an input from the user during runtime of applications. It's a very important function and used in almost all types of applications or programs.
So, let's try to see how we can use the input function. 
Python provides us input() function which accepts user input. You can then use this input within your program.
Python allows for user input, that means we are able to ask the user for an input.

Basic Example

       Use the prompt parameter to write a message before the input:
x = input('Enter your name:')
print('Hello, ' + x)
     Input() function takes the name which we give as an input("x")
Up until now, our programs were static. The value of variables was defined or hardcoded into the source code.
To allow flexibility, we might want to take the input from the user. In Python, we have the input() function to allow this.
Let's understand more the additional function about input function…..
The syntax for input() is:
Parameter        Description
prompt              A String, representing a default message before the input.
where prompt is the string we wish to display on the screen. It is optional.
>>> num
= input('Enter a number: ')

Enter a number:

>>> num

Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions.
>>> int('10')
>>> float('10')
This same operation can be performed using the eval() function. But eval takes it further. It can evaluate even expressions, provided the input is a string.
Note:- The input the function works with strings but with the help of additional functions this can convert the data type into the type conversion we command to the python coding.
I hope you enjoyed reading this article and finally, you came to know about User input in python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review