How to Use Python Input Function


In this example, we will learn how to use input function in Python. This function takes the input from the user and then convert it to string.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

a = input("input:")
# The default is the string type
print(type(a))

a = int( input("input:"))
# Cast to integer
print(type(a))

# Multiple inputs
a,b = map(int,input().split())  #The two sets of inputs are separated by spaces and mapped to integers using the map function.
print(a)
print(b)

# Example
while True:
    try:
        a,b = map(int,input().split())
        print(a+b)
    except:
        break;

Output:

input:3
<class 'str'>
input:3
<class 'int'>
3   7
3
7
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments