Taking input in Python

Python input() function is used to take user input. By default, it returns the user input in form of a string. 

Syntax: input(prompt)

The code prompts the user to input their name, stores it in the variable “name”, and then prints a greeting message addressing the user by their entered name.

Python3
name = input("Enter your name: ")
print("Hello,", name, "! Welcome!")

Output

Enter your name: w3wiki
Hello, w3wiki ! Welcome!

How to Change the Type of Input in Python

By default input() function helps in taking user input as string. If any user wants to take input as int or float, we just need to typecast it.

How to Print Names in Python

The code prompts the user to input a string (the color of a rose), assigns it to the variable color, and then prints the inputted color.

Python3
# Taking input as string
color = input("What color is rose?: ")
print(color)

Output

What color is rose?: Red
Red

How to Print Numbers in Python

The code prompts the user to input an integer representing the number of roses, converts the input to an integer using typecasting, and then prints the integer value.

Python3
# Taking input as int
# Typecasting to int
n = int(input("How many roses?: "))
print(n)

Output

How many roses?: 8
8

How to Print Float/Decimal Number in Python

The code prompts the user to input the price of each rose as a floating-point number, converts the input to a float using typecasting, and then prints the price.

Python3
# Taking input as float
# Typecasting to float
price = float(input("Price of each rose?: "))
print(price)

Output

Price of each rose?: 50.30
50.3

Find DataType of Input in Python

In the given example, we are printing the type of variable x. We will determine the type of an object in Python.

Python3
a = "Hello World"
b = 10
c = 11.22
d = ("Geeks", "for", "Geeks")
e = ["Geeks", "for", "Geeks"]
f = {"Geeks": 1, "for":2, "Geeks":3}


print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))

Output

<class 'str'>
<class 'int'>
<class 'float'>
<class 'tuple'>
<class 'list'>
<class 'dict'>



Input and Output in Python

Understanding input and output operations is fundamental to Python programming. With the print() function, you can display output in various formats, while the input() function enables interaction with users by gathering input during program execution.

Similar Reads

Python Basic Input and Output

In this introductory guide, we’ll explore the essentials of Python’s basic input and output functionalities, empowering you to efficiently interact with users and display information....

Print Output in Python

At its core, printing output in Python is straightforward, thanks to the print() function. This function allows us to display text, variables, and expressions on the console. Let’s begin with the basic usage of the print() function:...

Taking input in Python

Python input() function is used to take user input. By default, it returns the user input in form of a string....