In this tutorial, we will learn about python input() method and its uses with examples.

Python input() Method

The python input() method allows users to take input and store the imputed value in a variable as a string.

The syntax of input() method is:

input(prompt)

Python input() Method Parameters

The input() method takes only one optional argument:

  • prompt (Optional) – a setting that represents a message before the input. (By default, it will take input as a string.

Let’s see the same example of the input() method in python.

You can also create python projects like Dice Rolling Simulator in Python and  Pig Game in Python using only input() python method.

Example 1: How to use the input() method in python?

# Taking input from a user without any message

user_Input = input()

print("The inputted value is:", user_Input)

Output:

Python
The inputted value is: Python

Example 2: Input() method with prompt message.

# Taking input from the user with a message

user_Input = input("Enter anything:")

print("The inputted value is:", user_Input)

The output will be as follow:

Enter anything: Python
The inputted value is: Python

In the above program, we can see that when we run the program is returning a prompt, and in the same line, we can input the value, and after that, it is storing it in the user_Input variable.

The input() will automatically take input as a string, but we can use int() and float() to take input as in an integer and float. 

You can also see an amazing python blog on How to send birthday wishes email with python to send birthday wishes to  your family and friends.

Example 3: Taking input as integer and float in input() method.

# Taking input in integer and float

user_Input = int(input("Enter anything:"))

print("The inputted value is:", user_Input)
print(type(user_Input))


user_Input = float(input("Enter anything:"))

print("The inputted value is:", user_Input)
print(type(user_Input))

The output will be as follows.

Enter anything:25
The inputted value is: 25
<class 'int'>
Enter anything:25
The inputted value is: 25.0
<class 'float'>

This is how you can take input from users into different data types.

Rules of input() method

  • The input() method reads a line from the input (usually from the user), converts the line into a string by removing the trailing newline, and returns it.
  • If EOF is read, it raises an EOFError exception.

 

Was this article helpful?
Share on: