Simple Humble
Simple Humble

Reputation: 9

Python 2.x.x how to add a comma in print statement of python without preceding space character?

I am new to python and learning,

Code:

name = "Andrew"
print name,","

Output:

Andrew ,

A space character is injected in before the comma, which is undesirable. How may we print the comma immediately after name like Andrew, with no intervening space character?

Upvotes: 0

Views: 120

Answers (1)

Ahmed Akhtar
Ahmed Akhtar

Reputation: 1463

The default behavior of the print command of Python is to add a space between its arguments.

In Python 2.x.x:

You could use the string concatenation operator + to do this before you print it like:

name = "Andrew"
print name + ","

Output:

Andrew,

However, if you are dealing with Python 3.x.x you could use the argument sep to change the separator from space to empty character like:

print(name, ",", sep="")

Upvotes: 3

Related Questions