Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python input() Function



ThePython input() function provides a way to accept input from the user. It holds the program's execution and waits for the user to provide the required input. You can also use the prompt parameter to display a custom message to the user before the input.

Theinput() function is one of the commonly usedbuilt-in functions, and you can use this method directly without importing anymodule. It accepts any type of data but returns the given data in the string format.

There are some functions that are used withinput() (such asint(),float(), etc.) to convert the given input in a specific type of format.

Syntax

Following is the syntax of the Pythoninput() function −

input(message)

Parameters

The Pythoninput() function accepts a single parameter −

  • message − It represents aString which provides information related to user input.

Return Value

The Pythoninput() function returns a String containing the user input.

input() Function Examples

Practice the following examples to understand the use ofinput() function in Python:

Example: Use of input() Function

The following example shows how to use the Pythoninput() function. Here we are defining a string and applying the input() function to convert it into a String with input characters. The result will be the same as original string as it already has ASCII characters.

orgName = input("Enter your organisation name: ")print(f"Welcome to, {orgName}")

On running the above program, it will ask to enter organisation name. When we press enter after entering the name, it will print the following result −

Enter your organisation name: TutorialspointWelcome to, Tutorialspoint

Example: Integer and Float Input Using input() Function

To take input ofdata types in Python, we need to use the input() function by wrapping it into the corresponding type conversion function. In this example, we are taking input of integer and float type.

numbOne = float(input("Enter first num: "))numbTwo = int(input("Enter second num: "))addition = numbOne + numbTwoprint(f"The sum of {numbOne} and {numbTwo} is {addition}")

When we run the above program, it will prompt users to enter values −

Enter first num: 25.6Enter second num: 52The sum of 25.6 and 52 is 77.6

Example: Continuously Prompt for User Input

In the following example, the program will continuously ask the user for input until they type "exit".

while True:   userInp = input("Enter 'exit' to quit: ")   if userInp.lower() == "exit":      break   else:      print("Entered value:", userInp)

When we run the above program, it will prompt users to enter values −

Enter 'exit' to quit: 5Entered value: 5Enter 'exit' to quit: TPEntered value: TPEnter 'exit' to quit: exit
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp