Original article URL https://hackernoon.com/a-beginners-guide-to-getting-user-input-in-python-141q312q
This is a translation for self-study, so please use it at your own risk.
January 8th 2021 January 8, 2021
@aswinbarath Aswin Barath Budding Software Engineer @aswinbarath Oswin Ballas Rookie software engineer
Getting input from the user is what makes a program more interactive with the user. Accepting input from the user makes the program more interactive to the user.
Hence, in python we have an input function: input(), to receive input from the user.
So, to receive input from the user, Python has a function input ()
for input.
Take a look at an example. See the sample code below.
Output: The screen output looks like this:
Enter any data:Happy new year!
Happy new year!
When input() function is encountered by python, the program will pause until the user enters data.
When the Python interpreter executes the input ()
function, the program pauses until the user inputs data.
Later, any input entered by the user will be converted into a string. Let’s see another example to understand this point. Then, when the user enters some data, that data is converted to a string object. To understand this, take a look at the following sample code.
Output: The screen output looks like this:
Enter any text: Have a great year ahead.
text: Have a great year ahead. , type: <class 'str'>
Enter any number: 2021
number: 2021 , type: <class 'str'>
So, in these cases make sure that you convert the input to your preferred data type using its corresponding constructors. Let’s see that example too. Therefore, in the above cases, it is necessary to convert the input data to the desired data type using the corresponding constructor (in this case, the conversion method if the beginner reads it). See also the sample code below.
Output: The screen output looks like this:
Enter any number: 2021
number: 2021 , type: <class 'int'>
So, code along and have fun :) So let's enjoy coding! (^ v ^)
Recommended Posts