How To Take Input From User In Python?

Use `input()` to read a line of text from the user

Example: `name = input()`

Use a prompt inside `input()` to ask the user for input

Example: `name = input(“Enter your name: “)`

Convert input to an integer with `int()`

Example: `age = int(input(“Enter your age: “))`

Convert input to a float with `float()`

Example: `price = float(input(“Enter price: “))`

Read multiple values from one line using `split()`

Example: `a, b = input().split()`

Convert multiple values while reading them

Example: `x, y = map(int, input().split())`

Read a list of integers from one line

Example: `nums = list(map(int, input().split()))`

Read multiple lines using repeated `input()` calls

Example: `line1 = input()`

Example: `line2 = input()`

Use `sys.stdin.readline()` for faster input

Example: `import sys`

Example: `data = sys.stdin.readline().strip()`

Suggested for You

Trending Today