سؤال

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?

هل كانت مفيدة؟

المحلول 3

I like to concatenate the strings to avoid unwanted spaces:

print(name + ', you won.')

نصائح أخرى

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))

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.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top