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
-
Printing a single string:
print("Hello")
Returns Hello
-
Printing multiple strings:
print("Hello", "World")
Returns Hello World
-
Printing two strings while specifying the separator parameter:
print("Hello", "World", sep="__")
Returns Hello__World
-
Printing a string while specifying the end parameter:
print("geekbits", end=" to") print(" the world!!")
Returns geekbits to the world!!
-
Printing an array:
cars = ["Mustang", "Ferrari", "Lamborghini"] print(cars)
Returns ['Mustang', 'Ferrari', 'Lamborghini']
-
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!