Python print()

Python print() builtin function is used to print object(s) to the standard output or to a file if specified, with some options like separator, end character, and flush.

In this tutorial, we will learn about the syntax of Python print() function, and learn how to use this function with the help of examples.

Syntax

The syntax of print() function is

</>
Copy
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

where

ParameterRequired/OptionalDescription
objectsOptionalAny python object(s).
sepOptionalA string. Separator between objects printed out.
endOptionalA string. Printed out after all the objects are printed, as an ending.
fileOptionalA Python object with write(string) method implemented.
flushOptionalA flag that determines whether the stream has to be forcibly flushed or not. If True, the stream is forcibly flushed.

Returns

The function returns None.

Example

1. Print a string

In this example, we will pass a Python objects like string for objects parameter of print() function and observe what happens when the program is run.

Python Program

</>
Copy
object1 = 'abcde'
print(object1)

Output

abcde

The given string object is printed to the console.

2. Print Multiple Objects

In this example, we will pass some Python objects like string and number for objects parameter.

Python Program

</>
Copy
object1 = 'abcde'
object2 = 25.6
print(object1, object2)

Output

abcde 25.6

3. print() – Specific Separator

By default, the separator is a single space. So, if we do not specify an separator, the objects are print with single space as separator between them.

In this example, we will specify separator sep for print() function. We will print two string objects with hyphen '-' as separator.

Python Program

</>
Copy
object1 = 'abcde'
object2 = 'xyz'
print(object1, object2, sep='-')

Output

abcde-xyz

4. print() – Specific End

By default, the end parameter is a new line. So, if we do not specify end, the objects are print and a new line is print after that.

In this example, we will specify end parameter for print() function. We shall print two string objects and print ‘.\n’ after printing the objects using end parameter of print() function.

Python Program

</>
Copy
object1 = 'abcde'
object2 = 'xyz'
print(object1, object2, end='.\n')

Output

abcde xyz.

Conclusion

In this Python Tutorial, we have learnt the syntax of Python print() builtin function, and also learned how to use this function, with the help of Python example programs.