user6201066
user6201066

Reputation:

How to print a variable and a string in Python without a space between

My code is the following:

a = 60

print(a, ": string of text")

this prints "60 : string of text"

I would like it to print "60: String of text" without the space after the 60 if that makes sense

any ideas?

Upvotes: 2

Views: 1049

Answers (3)

cdarke
cdarke

Reputation: 44354

There are several ways to remove the space delimiter. Fortunately print() only inserts a space between fields by default. From the print() doc:

"sep: string inserted between values, default a space."

So my preferred way would be to use an empty string:

print(a, ": string of text", sep="")

Upvotes: 2

товіаѕ
товіаѕ

Reputation: 3244

if you use print(... , ... , ...) the , will always insert a space.

you can bypass that if you just concatenate them with

print(str(a) + ": string of text") 

(the str(..) is needed because a is a number) or you can use placeholders like so

print("%d: string of text" % a)

.. where %d stands for a digit. if you want a string, use %s

hope this helps :)

Upvotes: 0

user3820991
user3820991

Reputation: 2600

print("{}: string of text".format(a))

Upvotes: 0

Related Questions