Home > Mobile >  Python: How to remove empty quotes and a new line from the beginning of the string?
Python: How to remove empty quotes and a new line from the beginning of the string?

Time:10-16

Below is the string I'm operating on.

'exampleURL': ' '
                               'https://someurl.com'

I'm trying to clean it up using the code below. But for some reason, it doesn't work.

value_to_keep = (value_to_keep.rstrip('"').lstrip('"').rstrip("\n").lstrip("\n").rstrip("'").lstrip("'"))

I want the desired output to be

'exampleURL': 'https://someurl.com'

What is it that I'm missing here?

CodePudding user response:

Answering my own question. I have solved the issue by improving the pattern.

rstrip("' '").lstrip("' '")

Instead of one single quote, I have added two with a space. Because, that's how it is in the string.

CodePudding user response:

You can simply use the replace method and replace(" ", "")

    foo = {
           'exampleURL': '                           
       https://someurl.com'.replace(" ", "")
    }

print(foo['exampleURL'])

Output: https://someurl.com
  • Related