'Removing Space between variable and string in Python

My code looks like this:

name = Joe
print "Hello", name, "!"

My output looks like:

Hello Joe !

How do I remove the space between Joe and !?



Solution 1:[1]

a comma after print will add a blank space.

What you need to do is concatenate the string you want to print; this can be done like this:

name = 'Joe'
print 'Hello ' +  name + '!'

Joe must be put between quotes to define it as a string.

Solution 2:[2]

>>> print (name)
Joe
>>> print('Hello ', name, '!')
Hello  Joe !
>>> print('Hello ', name, '!', sep='')
Hello Joe!

Solution 3:[3]

You can also use printf style formatting:

>>> name = 'Joe'
>>> print 'Hello %s !' % name
Hello Joe !

Solution 4:[4]

One other solution would be to use the following:

Print 'Hello {} !'.format(name.trim())

This removes all the leading and trailing spaces and special character.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Reblochon Masque
Solution 2 PJProudhon
Solution 3 heemayl
Solution 4 Avneesh