.strip function not working in VS code (python)

I’m trying to get rid of whitespace in my code. Ex: name = name.strip()
It initially worked but now it is refusing to work.
Is this a path issue? I’m a beginner and I’m lost.

.strip is a method of str, so it’s not a path issue.

Please print out and post the before and after so that we can see what you’re seeing. It might be clearer if you use the ascii function when printing:

name = "whatever"
print("Before:", ascii(name))
name = name.strip()
print("After:", ascii(name))

You can preserve the formatting of the output in the post by selecting it and pressing the </> button.

What happens, and what did you expect to happen?

I expected it to get rid of the whitespaces between the name that is manually input by the user. When I run the program and enter my name as User Name, the white spaces still remain, and reads as User Name instead of User Name.

This was the output:
Before: ‘whatever’
After: ‘whatever’

This example doesn’t make sense to me as written (“User Name” instead of “User Name” ??), but it sounds like you think strip will remove internal whitespace, when it only removes the whitespace at the ends of the string. Per the documentation:

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None , the chars argument defaults to removing whitespace.

If you want to remove all the spaces, I would look at str.replace with an empty string as the second argument (e.g. name.replace(" ", "")). If you want to remove more than just spaces (e.g. tab characters), you will need to do something more complex.

My apologies, the spaces I put between User and Name were auto corrected but yes, that’s exactly what I meant. Thank you.

Keep in mind that if you remove all spaces from

User     Name

(on this forum, you need a full code block to preserve multiple spaces)

then you will get UserName, not User Name.

If you want User Name then that is normalizing whitespace, not removing it.

There is a clever trick for that. The .split method makes a list of all the words - the non-whitespace parts - which can then be .joined together with spaces in between. That could look like:

example = 'User    Name'
print(' '.join(example.split()))