Python – Print Output using print() function

Python – Print Output using print() function

Introduction

The print() function in Python is used to display messages to the screen or any other standard output device. In this article, we will explore the print() function, its syntax, parameters, and various operations.

Basic Example of print() Function

print("Hello, World!")
        

Output:

Hello, World!

Print with Variables

You can also print variables using the print() function:

name = "Alice"
age = 25
print("Name:", name)
print("Age:", age)
        

Output:

Name: Alice
Age: 25

Syntax of print() Function

The syntax of the print() function is as follows:

print(value(s), sep=' ', end='\n', file=file, flush=flush)

Parameters:

  • value(s): Any value or multiple values to be printed, which will be converted to a string.
  • sep='separator': (Optional) Specifies how to separate the objects if there is more than one. Default is a space.
  • end='end': (Optional) Specifies what to print at the end. Default is a newline.
  • file: (Optional) An object with a write method. Default is sys.stdout.
  • flush: (Optional) A Boolean indicating if the output is flushed (True) or buffered (False). Default is False.

Using the print() Function

When using the print() function, it is not necessary to pass any arguments, but you need empty parentheses to execute the function.

Example of Concatenating Strings

print("Hello, my name is", name, "and I am", age, "years old.")
        

Output:

Hello, my name is Alice and I am 25 years old.

String Literals in print()

You can format strings using string literals:

print("Example\nof new line.")
        

Output:

Example
of new line.

Using the end Parameter

The end parameter allows you to specify what to print at the end of the output:

print("This is the first line", end="; ")
print("and this is the second line.")
        

Output:

This is the first line; and this is the second line.

Using the sep Parameter

The sep parameter separates multiple outputs:

print("Hello", "World", sep="-")
        

Output:

Hello-World

Writing to a File

You can also write output to a file using the file parameter:

with open('output.txt', 'w') as f:
    print('Writing to a file.', file=f)
        

Formatting Output

There are several ways to format output in Python:

  • Using the format() method:
  • name = "Alice"
    age = 30
    print("Name: {}, Age: {}".format(name, age))
                
  • Using f-strings (Python 3.6+):
  • name = "Alice"
    age = 30
    print(f"Name: {name}, Age: {age}")
                
  • Using the % operator:
  • name = "Alice"
    age = 30
    print("Name: %s, Age: %d" % (name, age))
                

Conclusion

The print() function is a fundamental part of Python that allows you to display output in various formats. By utilizing its parameters, you can customize how your output appears, whether on the screen or in a file. Mastering the print() function is essential for effective coding in Python.