Python print() Function
The print() function prints the specified message to the screen or the text file. The message can be a string, or any other object (such as number, list, tuple etc.)
Syntax
print(*objects, sep, end, file, flush)
print() Parameters
Python print() function parameters:
| Parameter | Condition | Description |
|---|---|---|
*objects | Optional | Zero or more objects to print. |
sep | Optional | A string to print between each object. Default is a single space ' ' |
end | Optional | A string to print at the end. Default is a newline '\n'. |
file | Optional | An object with a write(string) method. Default is sys.stdout (screen) |
flush | Optional | If True, output buffer is forcibly flushed |
sep, end, file and flush must be specified as keyword arguments.
For example, sep = '-' or end = ' '.
This because you can have zero or more objects, e.g. print(object1, object2, ..., sep='-', end=' ').
If no values are specified, print() function will just print the default end parameter.
print() Return Value
Python print() function returns None (i.e. it does not return any value).
Examples
Example 1: Basic Example with String
Let's print the string 'Hello World!' on screen
print('Hello World!') # Output: Hello World!
output
Hello World!
Example 2: Printing Multiple Objects
You can print as many values as you like, just separate them with a comma ,.
print('One','Two','Three','Four','Five') # Output: One Two Three Four Five
output
One Two Three Four Five
Example 3: Printing with Custom Separator
When you print multiple values, each value is separated by default with a space ' '.
By specifying sep parameter, you can separate each value by something different from a space.
For example, let's separate each value with '-'
print('One','Two','Three', sep='-') # Output: One-Two-Three
output
One-Two-Three
You can choose any string you want as separator.
print('One','Two','Three', sep=' @WoW# ') # Output: One @WoW# Two @WoW# Three
output
One @WoW# Two @WoW# Three
Example 4: Ending with a Custom String
The print() function includes a newline \n at the end by default.
For example, to print the values without a trailing newline, specify end parameter.
print('First line.', end=' ') # print WITHOUT trailing newline
print('Next line') # print as usual with trailing newline
output
First line. Next line
Example 5: Printing Objects
You can print any object other than string such as number, list, tuple, etc. The object is converted into a string before written to the screen.
For example, you can print numbers:
# Print a list
print(1, 2, 3, 4, 5) # Output: 1 2 3 4 5
output
1 2 3 4 5
For example, you can print lists:
print([1, 2, 'red']) # Output: [1, 2, 'red']
output
[1, 2, 'red']
For example, you can print tuples:
print((1, 2, 'red')) # Output: (1, 2, 'red')
output
(1, 2, 'red')
Example 6: Printing to a File
In Python, you can print objects to the file by specifying the file parameter.
For example,
sourceFile = open('myFile.txt', 'w')
print('Write this in the file!', file = sourceFile)
sourceFile.close()
or, the same example using the with statement:
with open('myfile.txt', 'w') as sourceFile:
print('Write this in the file!!', file=sourceFile)
Example 7: Printing a Custom Object
To print custom objects in Python, you can override the __str__() method in your class: this method returns a string that represents the object.
When you print an object, Python automatically calls the __str__() method to determine what to display.
For example, let's print an instance of class Person:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"Person(name={self.name}, age={self.age})"
p = Person("Tom", 25)
print(p)
output
Person(name=Tom, age=25)