Summary of print output format in Python

Summary of Print Output Formats in Python

Summary of Print Output Formats in Python

1. Introduction

In programming, printing is a common method for debugging and outputting information. In Python, we can use the print statement to print output. This article will delve into controlling the output format of the Python print function.

2. Basic Usage

The Python print function is a built-in function that prints data to the standard output device (usually the console). Its basic syntax is as follows:

print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)

<p>Where objects is one or more objects to be printed, sep is a string used to separate multiple objects and defaults to a space, and end is the end of output character and defaults to a newline. Other parameters are beyond the scope of this article. </p>
<p>Here is a simple example:</p>
<pre><code class="language-python line-numbers">x = 10
y = 20
print("x =", x, "y =", y)

The output is:

x = 10 y = 20

3. Formatting Output

The Python print function provides several formatting options to control the output. These options include format strings (format placeholders) as well as alignment, padding, and precision. We’ll explain each of these options below.

3.1 Format String

A format string is defined using the % symbol after the object in the print function. A format string consists of regular characters and format placeholders.

Common formatting placeholders include:

  • %d: Placeholder for integers
  • %f: Placeholder for floating-point numbers
  • %s: Placeholder for strings

Here is an example:

name = "Alice"
age = 25
print("My name is %s and I am %d years old." % (name, age))

The output is:

My name is Alice and I am 25 years old.

3.2 Alignment and Padding

Alignment and padding options control the position and width of objects in the output.

Alignment options include:

  • <: Left alignment
  • >: Right alignment
  • ^: Center alignment

The fill option allows you to use any character for padding. By default, the fill character is a space.

Here is an example:

x = 10
y = 20
print("x = %2d, y = %2d" % (x, y))
print("x = %-2d, y = %-2d" % (x, y))
print("x = %02d, y = %02d" % (x, y))

The output is:

x = 10, y = 20
x = 10, y = 20
x = 10, y = 20

3.3 Precision

The precision option controls the number of decimal places in floating-point output.

The precision format is ., where represents the number of decimal places to retain.

The following is an example:

pi = 3.141592653589793
print("pi = %.2f" % pi)
print("pi = %.5f" % pi)

The output is:

pi = 3.14
pi = 3.14159

4. Printing to a File

In addition to printing to the standard output device (usually the console), we can also redirect the output to a file.

In the print function, we can specify the file object to which the output is to be sent using the file parameter.

Here is an example:

f = open("output.txt", "w")
print("Hello, World!", file=f)
f.close()

This code prints the string “Hello, World!” to a file named output.txt.

5. Conclusion

This article introduced output formatting control using the print function in Python. We learned basic print output usage and how to use format strings, alignment, padding, and precision options to control the output style. We also learned how to redirect output to a file.

Leave a Reply

Your email address will not be published. Required fields are marked *