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?
Submitted by IncludeHelp, on June 14, 2020

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)

Python examples with 'sep' parameter in print()

Example 1:

# 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:

# 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.