Menu Close

Python input() Function

Python input function

In this tutorial, you will go through the Python input() function to ask for input from the users. Being a Python developer or programmer, most of the time our requirement is to ask for values from the user’s side instead of defining hardcoded, in that situation input() function is used.

Python input() function Introduction

Python input() function is a built-in function that is used to ask for input from the users. input() function comes with Python by default. This function is most of the time used by Python programmers.

Syntax

The syntax of python input() function is:-

input(prompt)

Parameter

input () function in Python accepts one parameter.

  • prompt – A String, representing a default message before the input.

input() Return Value

By default, the input() function returns a string value.

Python input example

Let’s understand the input() function along with a few examples so that you can get more clarity about the input() function.

Example: Taking the name of the user as an input

name = input('Please enter your name:- ')
print(f"Your name is:- {name}")

Output

Please enter your name:- Vishvajit
Your name is:- Vishvajit

Example: Taking an integer from the user as an input

In this example, I am about to take two integers from the user as input and finally add them. During taking integer input from the user, you have to use the int() function to convert the string integer to int because as we know that by default, the input() function returns a string.

x = int(input("Enter first input:- "))
y = int(input("Enter second input:- "))

print(f"Addition of {x} and {y} is:- {x+y}")

Output

Enter first input:- 12
Enter second input:- 21
Addition of 12 and 21 is:- 33

Conclusion

So, Throughout this guide, we have explored the input() function to take input from the user to process that value. The input function in Python is the most useful function to ask for any value from the user. input() function always returns a string value.
If you like this article, please share and keep visiting for further python built-in functions tutorials.

Python built-in functions


For more information:- Click Here

Thanks for your valuable time… ❤️❤️🙏🙏

Python id() Function
Python hasattr() Function

Related Posts