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?
The String. trim() method # You can call the trim() method on your string to remove whitespace from the beginning and end of it. It returns a new string.
Python String rstrip() Method The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.
Method #1 : Using loop + remove() + endswith() Method.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With