A Beginner's Guide to Getting User Input in Python

Written by aswinbarath | Published 2021/01/08
Tech Story Tags: python | python-programming | python3 | python-tutorials | python-top-story | learn-python | python-tips | python-basics | web-monetization

TLDR Getting input from the user is what makes a program more interactive with the user. In python we have an input function: input() to receive input from a user. When input() function is encountered by python, the program will pause until the user enters data. Later, any input entered by the user will be converted into a string. In these cases make sure that you convert the input to your preferred data type using its corresponding constructors. Let’s see another example to understand this point.via the TL;DR App

Getting input from the user is what makes a program more interactive with the user.
Hence, in python we have an input function: input(), to receive input from the user.
Take a look at an example.
Output:
Enter any data:Happy new year!
Happy new year!

Input Function Under the Hood

When input() function is encountered by python, the program will pause until the user enters data.
Later, any input entered by the user will be converted into a string. Let’s see another example to understand this point.
Output:
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.
Output:
Enter any number: 2021
number: 2021 , type: <class 'int'>
So, code along and have fun :)

Written by aswinbarath | Community Leader | Web Developer | Blogger | Graphic Designer
Published by HackerNoon on 2021/01/08