Python Print() Function

Python Print() Function

The Python print function is one of the most commonly used functions in the language. It allows you to print items to the screen/console or a file, such as strings, numbers, etc. It can also print variables or expressions.

The function takes a single argument, which can be either a string or an object. Passing an object to the function converts it to a string before printing.

You can include other arguments in the function, but they are optional.

Syntax

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

print(item1, item2, ... , sep=' ', end='', file=sys.stdout)

The items in parenthesis are the values that you want to print.

The sep parameter(separator) specifies the separator between the items (the default value is a space)

The end parameter(end) specifies what should print at the end (the default value is a newline)

The file parameter specifies where the output prints (the default value is sys.stdout, which prints to the screen).

Examples

  1. Printing a single string:

    print("Hello")
    

    Returns Hello

  2. Printing multiple strings:

    print("Hello", "World")
    

    Returns Hello World

  3. Printing two strings while specifying the separator parameter:

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

    Returns Hello__World

  4. Printing a string while specifying the end parameter:

    print("geekbits", end=" to")
    print(" the world!!")
    

    Returns geekbits to the world!!

  5. Printing an array:

    cars = ["Mustang", "Ferrari", "Lamborghini"]
    print(cars)
    

    Returns ['Mustang', 'Ferrari', 'Lamborghini']

  6. Printing a tuple:

    cars = ("Mustang", "Ferrari", "Lamborghini")
    print(cars)
    

    Returns ('Mustang', 'Ferrari', 'Lamborghini')

Conclusion

The python print function is an essential tool for any Python programmer. It prints strings, numbers, objects, and so on to the screen. It also provides various optional parameters that give you more control over the output. Questions?? Put them in the comments.

Thanks for reading!

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.