Skip to content

Python print format

  • by

In Python, “print format” refers to the way you display output using the print() function. It involves formatting the data you want to print to the console or a file in a structured and readable manner.

In Python, the print function is used to display output. The general syntax of the print function is as follows:

print(value1, value2, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Let’s break down the components of the print function:

  1. value1, value2, …: These are the values or expressions that you want to print. You can pass multiple values separated by commas, and print will display them with a space (or a custom separator if specified) between them.
  2. sep: (Optional) The separator between values. By default, it is set to ' ', which means it will add a space between the values. You can change it to any other string if you want a different separator.
  3. end: (Optional) The string appended after all the values have been printed. By default, it is set to '\n', which means it adds a newline character at the end. If you want to suppress the newline character, you can set end=''.
  4. file: (Optional) The file object to which the output will be written. By default, it is set to sys.stdout, which means the output will be printed to the console. You can specify a different file object to write the output to a file.
  5. flush: (Optional) If set to True, the output will be flushed immediately, meaning it will be written to the specified file or the console right away. By default, it is set to False.

Examples:

# Basic usage
print("Hello, world!")  # Output: Hello, world!

# Multiple values with a custom separator
name = "John"
age = 30
print(name, age, sep=' - ')  # Output: John - 30

# Suppress newline character
print("Hello", end=' ')
print("World!")  # Output: Hello World!

# Writing output to a file
with open('output.txt', 'w') as f:
    print("This is written to a file.", file=f)

There are several methods to achieve print formatting in Python, including:

f-strings (Formatted String Literals) – Python 3.6 and above: f-strings offer a concise and readable way to format strings, allowing you to directly embed expressions inside curly braces {}.

name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")

String Concatenation: You can use the + operator to concatenate strings and variables together before printing.

name = "John"
age = 30
print("My name is " + name + " and I am " + str(age) + " years old.")

%-formatting (Old Style): The % operator can be used to format strings by specifying placeholders and providing values to replace them.

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

str.format() Method: The str.format() method provides a more flexible way to format strings. You can use placeholders inside curly braces {} and then pass the values to replace them.

name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))

Python print format example

Here’s an example that demonstrates different methods of print formatting in Python in a single code snippet:

# Data
name = "John"
age = 30

# Using String Concatenation
print("String Concatenation: My name is " + name + " and I am " + str(age) + " years old.")

# Using %-formatting (Old Style)
print("%%-formatting: My name is %s and I am %d years old." % (name, age))

# Using str.format() Method
print("str.format() Method: My name is {} and I am {} years old.".format(name, age))

# Using f-strings (Formatted String Literals) - Python 3.6 and above
print(f"f-strings: My name is {name} and I am {age} years old.")

# Using print function arguments
print("Print Function Arguments:", "My name is", name, "and I am", age, "years old.")

Output:

Python print format

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

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