Open In App

Python String Input Output

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python String Input Output Operation

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

  • Input Operation in Python
  • Output Operation in Python

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

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:

Example 1: Basic Print Statement in Python

The most basic type of output in Python is printing information to the console with the print() function. You can print strings, variables, and expressions as well as modifying the output with formatting choices.

Python3
print("Hello World!")

Output
Hello World!

Example 2: Print Output with End Parameter

In Python, the print() method is used to display output on the console. The end logic allows you define the character(s) that will show up at the end of the output. The default value for end is ‘\n’, which represents that the output will end with a newline character. You may change this behavior by providing a different value to the end argument. Here’s how to use the end parameter in the print() function:

In this example, all print() commands have the end logic changed to a space”. Therefore, each following print() call will link its results to the same line without creating a new one. Finally, the output ends with a space because of the last print() statement’s end option, which defaults to ‘\n’.

Python3
# Print multiple values on the same line
print("Hello", end=" ")
print("world!", end=" ")
print("How", end=" ")
print("are", end=" ")
print("you?", end=" ")

Output
Hello world! How are you? 

Example 3: Printing Output with Separator Parameter

In Python, the print() process has a sep option that allows you to provide a separator between multiple values given to the print() function. By default, sep is set to a single space character”, suggesting that the values supplied to print() are separated by spaces. You may change this behavior by changing the sep logic to a different value.

In this example, the sep claim is set to “, “, ” – “, and ” | ” for every print() command. As a result, the values given to print() are separated by commas, hyphens, and vertical bars, instead of a normal single space character. You can use the sep option to change the formatting of your output and include any text as a separator between data. This allows you to format the output exactly as you want.

Python3
# Print multiple values with a custom separator
print("red", "blue", "grey", sep=", ")
print("one", "two", "three", sep=" - ")
print("Python", "Java", "C++", sep=" | ")

Output
red, blue, grey
one - two - three
Python | Java | C++

Example 4: Print Output with f-String

In Python, you can use format strings to change the output of the print() command. Format strings allow you to include variables and expressions within a string without describing how they should be formatted. To make use of format strings for output operations in Python, perform the following instructions:

In this example, below code sets variables for name, age, and height, then showcases three methods to format and print them: using format string, f-strings, and format string with custom formatting options. Each method outputs a formatted string displaying the values.

Python3
# Define variables
name = "Rahul"
age = 30
height = 175.5

# Using format string
print("Name: {}, Age: {}, Height: {}".format(name, age, height))

# Using f-string (Python 3.6+)
print(f"Name: {name}, Age: {age}, Height: {height}")

# Using format string with formatting options
print("Name: {:10s}, Age: {:3d}, Height: {:.2f}".format(name, age, height))

Output
Name: Rahul, Age: 30, Height: 175.5
Name: Rahul, Age: 30, Height: 175.5
Name: Rahul     , Age:  30, Height: 175.50

Frequently Asked Questions (FAQ)

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

The str() method in Python converts an integer to a string. For example, if you have a = 3 and want to convert it to a string, simply type a = str(3).

How do you get the output of a string in Python?

Take user input from the keyboard using the built-in function input(). Print output to the console using the built-in method print(). Use Python’s f-strings to format string data.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads