Thi G.
Thi G.

Reputation: 1668

Python - Removing space in a string

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?

Upvotes: 1

Views: 2551

Answers (3)

novski
novski

Reputation: 206

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.

Upvotes: 0

Jaylin
Jaylin

Reputation: 109

Use + instead of ,

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

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

Upvotes: 0

Rik Poggi
Rik Poggi

Reputation: 29322

Use string formatting:

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

or:

print 'your name is %s!' %name

Upvotes: 15

Related Questions