Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

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 …

Lokesh Gupta

October 1, 2022

Python Built-in Functions
Python Basics
Python

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:

input([prompt])

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.

howtodoinjava
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 !!

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python Examples

Python Built-in Functions

  • Python abs()
  • Python all()
  • Python any()
  • Python ascii()
  • Python bin()
  • Python input()
  • Python range()
  • Python print()

Python String Functions

  • String capitalize()
  • String count()
  • String endswith()
  • String split()
  • String startswith()

Table of Contents

      • Example: Python input() method to take user’s name from Console
  • Syntax of input() Method
      • Method Parameters
      • Method Return Value
  • Python input() Method Example
      • Example 1: Read user input without prompt message
      • Example 2: Read user input without prompt message
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Python pass Statement

Next

Python print() Method

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.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap