Python print() function
The print() is a built-in function used to print values on to the output stream in Python
The python provides a built-in function print() to perform output operation. The print() function can print any valid data type of value. The print() function has the following syntax.
print() syntax
print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False)
The print() function is used to print any data type of value like string, formatted string, integer, floating-point, complex numbers, boolean values, and None value.
The print() function prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout (console).
sep: string inserted between values, default a blank space.
end: string appended after the last value, default a new-line.
flush: whether to forcibly flush the stream.
The print() function has defined with the following function header.
print() function header
def print(*values: object,
sep: str | None = ...,
end: str | None = ...,
file: SupportsWrite[str] | None = ...,
flush: bool = ...) -> none
printing different data type of values in python
print("This is a string")
print(f'This is formatted string')
print(100) # Printing an integer value
print(100.99) # Printing a floating-point value
print(10+20j) # Printing a complex number
print(True) # Printing a boolean value
print(None) # Printing None value
Printing multiple data values
The print() function can be used to print more than one data value. That means, a print() function can accept more than one argument and prints all the arguments in a single line by separating each with blank space.
All the arguments of a print function may be of same data type or different.
printing multiple data values in python
print("Hello", "Python", "Coding") # output: Hello Python Coding
print(10, 20, 30) # output: 10 20 30
print(100, 'Python') # output: 100 Python
print(100.99, True, None) # output: 100.99 True None
Changing separator (sep = 'string')
The print() function has default argument sep with a default value blank space. We can pass any string as keyword argument to define our own separator as sep = 'separator'. The sep argument must be used after all the arguments that need to be print.
printing multiple data values with given separator in python
print("Hello", "Python", "Coding", sep = '-') # output: Hello-Python-Coding
print(10, 20, 30, sep = '') # output: 102030
print(100, 'Python', sep = '_*_') # output: 100_*_Python
print(100.99, True, None, sep = 'BSC') # output: 100.99BSCTrueBSCNone
The sep value must be a string (empty or non-empty), it does not accept any other data type value.
Changing ending string (end = 'string')
The print() function also has default argument end with a default value new-line. Every print() funciton terminates with a new-line character, this leads to start the next print() function printing from a new-line.
printing multiple data values with given separator in python
print("Hello", "Python", "Coding")
print(10, 20, 30)
# output:
# Hello Python Coding
# 10 20 30
We can pass any string as keyword argument to define our own ending string as end = 'string'. The end argument must be used after all the arguments that need to be print.
printing multiple data values with given separator in python
print("Hello", "Python", "Coding", end = '')
print(10, 20, 30)
# output:
# Hello Python Coding10 20 30
The end value must be a string (empty or non-empty), it does not accept any other data type value.
printing multiple data values with given separator in python
print("Hello", "Python", "Coding", end = '***')
print(10, 20, 30, end = 'XYZ')
# output:
# Hello Python Coding***10 20 30XYZ
The print() function return value
The print() function always returns a value None.
printing multiple data values with given separator in python
data = print("Hello", "Python", "Coding")
print(data)
# output:
# Hello Python Coding
# None
Display the given list of names with hyphen separation in python
names = ['Ajay', 'Surya', 'Seshu', 'Gouthami', 'Heyansh', 'Josh']
print(*names, sep = '-')
# output:
# Ajay-Surya-Seshu-Gouthami-Heyansh-Josh