5

I want to remove the whitespace at the end of 'Joe'

name = 'Joe'
print(name, ', you won!')
>>>Joe , you won!

I tried the rstrip method, but it didn't work

name='Joe'
name=name.rstrip()
print(name, ', you won!')
>>>Joe , you won!

My only solution was to concatenate the string

name='Joe'
name=name+','
print(name,'you won!')
>>>Joe, you won!

What am I missing?

0

3 Answers 3

11

The print() function adds whitespace between the arguments, there is nothing to strip there.

Use sep='' to stop the function from doing that:

print(name, ', you won!', sep='')

or you can use string formatting to create one string to pass to print():

print('{}, you won!'.format(name))
Sign up to request clarification or add additional context in comments.

2 Comments

You're so quick! LOL :)
@MartijnPieters got it ;)
3

The white space is being added by print because you're passing it two parameters and this is how it works.

Try this:

print(name, ', you won!', sep='')

Another way would be doing some string formatting like:

print('%s, you won!' % (name))    # another way of formatting.

Comments

2

I like to concatenate the strings to avoid unwanted spaces:

print(name + ', you won.')

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.