Input Operations in Python

As programmers, we sometimes have to provide the computer input while we are writing our code. Python’s input() method may be used for this. When the user enters data, the input() method always returns a string data type. If we want to change the data type of our inputs, we have to use a different conversion method.

Example 1: String Input in Python

The input() method in Python allows you to get user-supplied string input. The input() method asks the user to enter a string and returns the result as a string. In this example, the below code takes the user to input their name and stores it in the variable “name”. Then, it prints out the entered name with a descriptive message.

Python3
# Prompt the user to enter a string
name = input("Enter your name: ")

# Display the input string
print("You entered:", name)

Output

Enter your name: Pratham
You entered: Pratham

Example 2: Type Conversion For Input Operations

In Python, type conversion can be used to transform user input from one data type to another. This is useful when you need to get a data type (for example, integer or float) and perform operations on it. Here’s how you can do input operations using type conversion:

Python3
# Accept the length of the rectangle from the user
length = float(input("Enter the length of the rectangle: "))
print("Length of the rectangle:", length)

# Accept the width of the rectangle from the user
width = float(input("Enter the width of the rectangle: "))
print("Width of the rectangle:", width)

# Calculate the area of the rectangle
area = length * width

# Print the area of the rectangle
print("Area of the rectangle:", area)

Output

Enter the length of the rectangle: 90
Length of the rectangle: 90.0
Enter the width of the rectangle: 12
Width of the rectangle: 12.0
Area of the rectangle: 1080.0

Python String Input Output

Python’s print() method is used to show program output, whereas the input() function is used for collecting user input. Python starts to accept all input as strings; other data types require specific conversion. We will learn the fundamental input/output processes in Python in this tutorial. We’ll talk about many uses of the Python programming language’s input and output features in this section.

Similar Reads

Python String Input Output Operation

Below, we will discuss the Python String Input Output operation in these two sections:...

Input Operations in Python

As programmers, we sometimes have to provide the computer input while we are writing our code. Python’s input() method may be used for this. When the user enters data, the input() method always returns a string data type. If we want to change the data type of our inputs, we have to use a different conversion method....

Output Operations in Python

Output operations in Python involve providing information to the user in a variety of ways, including printing to the console, writing to files, formatting strings, and creating graphical user interfaces (GUIs). When creating our code, we must display the results of specific operations that are executed done in our code. We can do this with the print() method. The complete syntax for the print() function is:...

Frequently Asked Questions (FAQ)

How do you input a value into a string in Python?...