Methods and Functions

Python input()

The python input function is used to derive input from the user. The function reads the line of input, and converts the input into a string, and returns the output. 

python input

Source

The syntax of the input() is as follows:

input([prompt])

input() Parameters

The python input function takes only one argument as a parameter. This argument is optional. 

  1. prompt- It’s a string message that asks for input from the user. It is displayed on the output screen.

Return value from input()

The input() method takes a line from the user’s input, eliminates the trailing newline, and returns it as a string.

When the EOF is encountered, an EOFError exception is thrown.

Here are some examples:

Example 1:

We are writing a program to demonstrate how to take input from the user and display it.

Source Code:

                    

number = input("Enter a value: ")  

# Displaying the result  

print("You entered:",number)  



Output:



Enter a value: 60

You entered: 60

Example 2:

                    

number = input("Enter an integer: ")  

# Displaying result  

number = int(number) # casting into a string  

sqr = (number*number) # getting the square  

print("Square of the number:",sqr)  



Output:


Enter an integer: 7

Square of the number: 49

What is input () in Python?

The input () is a built-in function in python that retrieves the input from the user. 

For example: 

If you want to determine whether the candidate is eligible to vote or not, the input() is used by the user to enter his age. The input is entered by the keyboard and is used to receive information from the user. 

Example 1: How input() works in Python?

                    

# get the input from the user


inputString1 = input()



print('The string variable given as input is:', inputString1)



Output


Python is my favorite subject.

The string variable given as input is: Python is my favorite subject.

Example 2: Get input from user with a prompt

                    

# get the input from the user



input_String = input('Enter a string:')



print('The string variable given as input is:', input_String)



Output



Enter a string: Python is my favorite subject.

The string variable given as input is: Python is my favorite subject.

How do you Write Input in Python?

To make sure the programs that we write are interactive, python has an in-built function named input() that is designed to take the inputs from the user using the keyboard.

The input() takes the input from the user. The user must enter the input when the prompt message pops on the screen with the help of the keyboard. Once the input is given, this is evaluated and returned as a string. 

The compiler identifies the type of input. If the syntax of the input is incorrect, it raises a syntax error or an exception. 

The input must follow the following syntax:

input([prompt])

The prompt is an optional argument. The prompt argument pops a text on the output screen for the user to type his input values.

How do you input in Python 3?

The input() allows the users to enter a value into the program. The value returned by the input() is a string. Any data type can be used to convert the contents of an input.  

For example:

The user can convert the value entered into an integer variable. This is supported in Python3. 

Source Code

                    

email_add = input("Enter your email address: ")


print("To confirm, is your email address:", email_add)



Output



Enter your email address:



[email protected]

In the above program, we used the input() to ask the user for his email address. The value entered by the user is stored in the variable ’email_add.’ This value stored is then printed.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.