문제

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