Python Input Types with Examples
1. Basic String Input
Code:
s = input("Enter a string: ")
print(s)
Input:
Hello World
Output:
Hello World
2. Integer / Float Input
Code:
num = int(input("Enter an integer: "))
f = float(input("Enter a float: "))
print(num, f)
Input:
10
3.14
Output:
10 3.14
3. List of Integers (Array-style)
Code:
arr = list(map(int, input("Enter space-separated numbers: ").split()))
print(arr)
Input:
12345
Output:
[1, 2, 3, 4, 5]
4. List of Strings
Code:
Python Input Types with Examples
words = input("Enter words: ").split()
print(words)
Input:
apple banana mango
Output:
['apple', 'banana', 'mango']
5. 2D List (Matrix)
Code:
rows = int(input("Enter number of rows: "))
matrix = [list(map(int, input().split())) for _ in range(rows)]
print(matrix)
Input:
123
456
Output:
[[1, 2, 3], [4, 5, 6]]
6. Tuple Input
Code:
t = tuple(map(int, input("Enter tuple values: ").split()))
print(t)
Input:
10 20 30
Output:
(10, 20, 30)
7. Dictionary Input (Key-Value Pairs)
Code:
d = {}
Python Input Types with Examples
n = int(input("Enter number of items: "))
for _ in range(n):
key, value = input("Enter key and value: ").split()
d[key] = value
print(d)
Input:
name John
age 25
Output:
{'name': 'John', 'age': '25'}
8. Set Input
Code:
s = set(map(int, input("Enter unique elements: ").split()))
print(s)
Input:
12234
Output:
{1, 2, 3, 4}
9. Multiple Variables in One Line
Code:
a, b, c = map(int, input("Enter three numbers: ").split())
print(a, b, c)
Input:
5 10 15
Output:
5 10 15
10. List of Characters
Python Input Types with Examples
Code:
chars = list(input("Enter characters: "))
print(chars)
Input:
hello
Output:
['h', 'e', 'l', 'l', 'o']
11. Boolean Input
Code:
val = input("Enter true or false: ").lower() == "true"
print(val)
Input:
True
Output:
True
12. JSON-like Dictionary Input
Code:
import json
d = json.loads(input("Enter JSON: "))
print(d)
Input:
{"name": "Alice", "age": 30}
Output:
{'name': 'Alice', 'age': 30}
13. Input Using literal_eval
Code:
from ast import literal_eval
data = literal_eval(input("Enter a literal: "))
Python Input Types with Examples
print(type(data), data)
Input:
[10, 20, 30]
Output:
<class 'list'> [10, 20, 30]
14. Multiple Lines of Input
Code:
lines = []
for _ in range(3):
lines.append(input())
print(lines)
Input:
first line
second line
third line
Output:
['first line', 'second line', 'third line']
15. Read Until EOF (sys.stdin)
Code:
import sys
for line in sys.stdin:
print(line.strip())
Input:
Hello
World
Python
(then Ctrl+D)
Output:
Hello
Python Input Types with Examples
World
Python