1

So, I would like to print a string with an exclamation mark at the end of the statement but without any space between the last word and the exclamation mark. For instance:

name = raw_input('Insert your name: ')

Now, I would like python to print something like this:

Your name is John!

So, I type:

print 'Your name is', name, '!'

And it returns me:

Your name is John !

What I want to do is to remove the space between the "John" and the exclamation mark.

Any ideas?

2
  • 3
    print "Your name is %s!" % (name) Commented Apr 20, 2012 at 15:00
  • from subject, before I read the text, I was going to answer, " ".join(s.split()) Commented Apr 20, 2012 at 15:04

3 Answers 3

15

Use string formatting:

print 'Your name is {}!'.format(name)

or:

print 'your name is %s!' %name
Sign up to request clarification or add additional context in comments.

Comments

0

Use + instead of ,

print 'Your name is' + name + '!'

However, why will you remove the space between them, it's not a good idea.

Comments

0

I came along this old topic and thought I might know a newer way to do this:

print(f'Your name is {name}!')

This source tells it is part of the game since v3.6.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.