How to Convert a String to URL in Python

Capturing Web addresses inputted by users is a common task when developing Web applications in Python. You store the URL as a string, but need to normalize the address before calling it in a function. For example, the user could have made a typo when entering an address. If you pass this address with a typo or blank space characters in it, your program will not run properly. Luckily, Python contains a built-in module called "urllib" to automatically convert string data into a functional URL.

Instructions

    • 1

      Open your Python editor.

    • 2

      Type:

      "import urllib

      input_url = urllib.quote ( 'http://www.example.com/example two.html' ) "

    • 3

      Press "Return."

      In this example, Python loads the urllib module, then takes the string and normalizes the URL by replacing the unreadable blank space in the URL between "example two.html" with the special character "%20".

Related Searches

References

You May Also Like

Related Ads

View Blog Post

Enter the Geek Vs Geek Giveaway and Win a Roku!