Python built-in Method - input()

The input() method in Python is a built-in function that reads a line of text from standard input (usually the keyboard), and returns it as a string.

The syntax for input() is as follows:

re‮f‬er to:theitroad.com
input([prompt])

Here, prompt is an optional string that is displayed to the user before waiting for input.

For example:

name = input("Enter your name: ")
print("Hello, " + name + "!")

In the example above, we use the input() function to prompt the user to enter their name. The input is read from the keyboard and stored in the name variable. We then use the print() function to display a personalized greeting to the user.

The input() function is useful when you need to get user input from the command line, such as when building a command-line interface or a text-based game.

Note that the input() function always returns a string, even if the user enters a number or another data type. If you need to convert the input to a different data type, such as an integer or a floating-point number, you can use the appropriate conversion function, such as int() or float(). For example:

age_str = input("Enter your age: ")
age = int(age_str)

In the example above, we prompt the user to enter their age. We read the input as a string using the input() function and store it in the age_str variable. We then use the int() function to convert the input to an integer and store it in the age variable.