Python input function allows to user pass content in the program. In a simple word, the program can read the line form console, which entered by users. With the input function, we can read string and integer (Numbers) data types in python. In this tutorial, you will learn about the input function with examples.
Input() function syntax is very simple and easy look at:
- Prompt – It’s a string which showed up in console screen without trailing newline (Optional)
The input() method reads a line from the console (enter by the user), then converts the line into a string and return it.
Python input function Examples
This one example without using Prompt. Where in this program content reading from console and print with the sentence.
print('Enter your name:') inputName = input() print('Hello, ' + inputName)
Output: Enter your name:
Let’s see an example with Prompt
inputName = input('Enter your age: ') print('You Entered - ' + inputName)
Output: Enter your age: 209
You Entered – 209
Python Read Input Integer
The upper example is no restrictions on input type data. So what if you want only read the Integer type value from the console. You have to put int() function around the input function.
Here is an example of adding tow numbers of given by users.
in1 = int(input('Enter First number : ')) in2 = int(input('Enter Second number : ')) sum = in1 + in2 print(sum)
Output: Enter First number : 3
Enter Second number : 3
Note: if you added write string then it will throw an error.
ValueError: invalid literal for int() with base 10: 'xyz'
Do comment if you have any suggestions and doubts in this tutorial.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Examples of Python input function are in Python 3, so it may change its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.