Links

Convert string to URL

Tags: #python #urllib #string #url #convert #library
Author: Florent Ravenel
Last update: 2023-04-12 (Created: 2023-03-29)
Description: This notebook will show how to convert a string to a URL using urllib.
References:

Input

Import libraries

import urllib.parse

Setup Variables

  • string: string to be converted to URL
string = "This is a string"

Model

Convert string to URL

The quote() function replaces special characters in the string with their percent-encoded equivalent, which is necessary for URLs. The resulting URL can be used in your Python code or in a web browser. Note that if you need to convert the URL back to a string, you can use the unquote()
url = urllib.parse.quote(string)

Output

Display result

print(url)