Best & Quick Introduction To Python User Input Function

In this article, you will learn about Python User Input Function

Python User Input Function – Before moving ahead, let’s know a little bit about Python Error Keywords

Input function – It allows Python to take the user’s response or answer. In other words, a user can give his/her answer to Python and tell how to perform.

There are two ways to call the Input function in Python.

1. For Python version 2.7 – raw_input()

2. For Python version 3.9 or > – input ()

Let’s first get started with Python version 2.7

Example 1- Take input or response from the user and print the statement.

user_input = raw_input(' ' + 'Python World')
print (user_input)

As a result, it first takes a response from the user then adds it to the sentence, and finally prints the statement.

Now for Python version 3.9 or >

Example 2- Take str type input or response from the user and print the statement.

user_input = input(' ' + 'Python World')
print (user_input)
python-user-input-function

As a result, it first takes a response from the user then adds it to the sentence, and hence prints the statement.

Example 3- Take int type input or response from the user and print the statement.

user_input = int(input('Enter a number: '))
print('Addition of two numbers is :', user_input, '+', 5, '=', user_input + 5)
python-user-input-function

As a result, the first response was taken from the user then the number was added to the existing number and printed the statement.

Note: By default input() function takes response in str type, and an int type and str type cannot be added that’s why we added the int() function to change the type of input() function.

Example 4- Take int type input or response from the user and print the statement.

user_input = int(input('Enter a number: '))
print('Subtraction of two numbers is :', user_input, '-', 5, '=', user_input - 5)
python-user-input-function

As can be seen that the first response was taken from the user then the number was subtracted to the existing number and printed the statement.

Example 5- Take both str and int type of response from the user and print the statement.

user_str_input = input('Enter your name : ')
user_int_input = (input('Enter your age: ')

print("I'm " + user_str_input + ' and' + " my age is " + user_int_input + '.')
python-user-input-function

As shown above that first, the response was taken from the user, and then it was added in a sentence and printed the statement.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on