sep parameter in Python with print() function

Python | sep parameter in print(): In this tutorial, we are going to learn about the sep parameter with print() function, how does it work with print() function? By IncludeHelp Last updated : December 17, 2023

The 'sep' parameter in printf() function

sep parameter stands for separator, it uses with the print() function to specify the separator between the arguments.

The default value is space i.e. if we don't use sep parameter – then the value of the arguments is separated by the space. With the "sep", we can use any character, an integer or a string.

Note: "sep" is available in Python 3.x or later versions.

Syntax

print(argument1, argument2, ..., sep = value)

Examples

Example 1: Using different values for sep parameter

# variables
name = "Mike"
age = 21
city = "Washington, D.C."

# printing without using sep parameter
print("Without using sep parameter...")
print(name, age, city)
print()

# printing with using sep parameter
# separated by spaces
print("With using sep parameter (separated by spaces)")
print(name, age, city, sep=' ')
print()

# printing with using sep parameter
# separated by colon (:)
print("With using sep parameter (separated by colon)")
print(name, age, city, sep=':')
print()

# printing with using sep parameter
# separated by " -> "
print("With using sep parameter (separated by ' -> ')")
print(name, age, city, sep=' -> ')
print()

Output

Without using sep parameter...
Mike 21 Washington, D.C.

With using sep parameter (separated by spaces)
Mike 21 Washington, D.C.

With using sep parameter (separated by colon)
Mike:21:Washington, D.C.

With using sep parameter (separated by ' -> ')
Mike -> 21 -> Washington, D.C.

Example 2: Disable space separator, and using numbers/characters for sep values

# variables
name = "Mike"
age = 21
city = "Washington, D.C."

# printing with using sep parameter
# disable space separator 
print("With using sep parameter (disable space separator)")
print(name, age, city, sep='')
print()

# printing with using sep parameter
# separated by number
print("With using sep parameter (separated by number)")
print(name, age, city, sep='12345')
print()

# printing with using sep parameter
# separated by " ### "
print("With using sep parameter (separated by ' ### ')")
print(name, age, city, sep=' ### ')
print()

Output

With using sep parameter (disable space separator)
Mike21Washington, D.C.

With using sep parameter (separated by number)
Mike123452112345Washington, D.C.

With using sep parameter (separated by ' ### ')
Mike ### 21 ### Washington, D.C.

Python Tutorial

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.