WincyGaming
WincyGaming

Reputation: 29

Unwanted space in Python

name = raw_input ("What's your name? ")
print "Hello",name, "!"

It returns the following:

What's your name? John
Hello John !

How do I make it so that the space between John and ! doesn't appear?

This is in Python-2.7 by the way

Upvotes: 2

Views: 93

Answers (4)

Amen
Amen

Reputation: 1633

since your name variable itself is a string you can use these instead:

print "hello"+name+"!"

or if it was't you can cast it to string using the str() function.

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

Use the str.format() method:

print "Hello {0}!".format(name)

This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting [...] in new code.

Upvotes: 7

GHugo
GHugo

Reputation: 2654

Use format string:

print "Hello %s!"%name

Upvotes: 2

Mathias711
Mathias711

Reputation: 6658

You can do something like:

print 'Hello %s!' %(name)

This will return

Hello John!

Upvotes: 2

Related Questions