Skip to content

Python input function | Input String | Input Integer (Number)

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.

Python input function | Input String | Input Integer (Number)

Syntax

Input() function syntax is very simple and easy look at:

input(prompt)

Parameter Values

  • Prompt  – It’s a string which showed up in console screen without trailing newline (Optional)

Return value

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:
Eyehunt
Hello, Eyehunt

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 Input Integer Number

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
6

Note: if you added write string then it will throw an error.

ValueError: invalid literal for int() with base 10: 'xyz'

Python Read Input as Integer error

Do comment if you have any suggestions and doubts in this tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Examples of Python input function are in Python 3, so it may change its different from python 2 or upgraded versions.

2 thoughts on “Python input function | Input String | Input Integer (Number)”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading