Python input()

The Python input() function allows user inputs in the programs. The input() function reads a line from the input, converts into a string and returns it as method return value.

The input() function, optionally, allows a prompt String as the method argument that is displayed to user while taking the input.

Example: Python input() method to take user’s name from Console

In the given example, when asked in Console, I have input my name ‘Lokesh’.

x = input()
print('Hello, ' + x)

Program output.

Hello, Lokesh

Syntax of input() Method

The syntax of input() method is:


Method Parameters

prompt (optional): A String, representing a message before the input, in th standard output (usually screen). Ther is no trailing newline after the prompt message.

Method Return Value

The input() method reads a line input by user in the Console. It converts the line into a string by removing the trailing newline and returns it to the program.

If we try to read the EOF, it will throw an EOFError exception.

Python input() Method Example

Example 1: Read user input without prompt message

userInput = input()

print('The input string is:', userInput)

Program output.

The input string is: howtodoinjava

Example 2: Read user input without prompt message

inputNumber = input("Enter the number to get the square: ")

print('The square of {0} is: {1}'.format(
inputNumber, int(inputNumber) * int(inputNumber)))

Program output.

Enter the number to get the square: 5
The square of 5 is: 25

Happy Learning !!

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial