Search code examples
pythonpython-3.xurllibpython-webbrowser

How to remove %20 from url in python?


How do I remove %20 from url in my code?

elif 'launch' and 'open' in voice_note:
    print('opening...')
    start_url = "https://www."
    end_url = ".com"
    urllib.parse.unquote(voice_note)
    play_sound_from_polly('Displaying the result, sir')
    webbrowser.open(start_url + voice_note.replace('open', '').replace('%20', '') + end_url)
    exit()

Here is a image of what it opens image


Solution

  • Instead of doing this:

    webbrowser.open(start_url + voice_note.replace('open', '').replace('%20', '') + end_url)
    

    Use strip() function:

    webbrowser.open(start_url + voice_note.replace('open', '').strip() + end_url)
    

    Or simply put:

    webbrowser.open(start_url + voice_note.replace('open', '').replace(' ', '') + end_url)
    

    The reason is you are trying to replace '%20' and not a space. %20 is an encoded value of space in a url.