Unable to remove blanks/tabs from string

I am using Python to build the logic. Facing the challenge in removing the whitespaces in the string. Actual code as below:

searchExprLineWithoutSpace.replace(" ", "").strip())

Even i have triedwith lstrip, rstrip but no luck. Actual String in this is as below:

"// SetSearchExpr(searchExpr);
                ExecuteQuery();
                var FR = FirstRecord();"

Not matter what I do, I am unable to remove the space from the first line: enter image description here

If someone can steer as to what else I can do, it would help. Thanks.

Answer

If you want to remove all whitespace, you can use the generic paramter for str.split, then join it.

One thing I will note is that this string that you provided is incorrect, since it needs to be triple quote for multilines.

Anyways, if you want to remove the whitespace, you can do

white_space_to_remove = "hello       world this    is uneven    whitespace"
white_space_to_remove = ''.join(white_space_to_remove.split())

This will give the string with no whitespace whatsoever, since the delimiter for str.join is just an empty string.

However, I’m unsure as to what you’re trying to do with removing whitespace since your question was quite generic.

Note: str.split without any parameters will by default split by any whitespace.