Posted on • Originally published attech.serhatteker.com on
Convert Strings To Be URL Safe
In order to convert your strings to be URL safe you can use python's built-inurllib
:
>>>fromurllibimportparse>>>string_raw="Test: Hey! this is an example string">>>parse.quote(string_raw)'Test%3A%20Hey%21%20This%20is%20an%20example%20string'
quote()
replaces special characters in string using the%xx
escape. Letters, digits, and the characters_.-~
are never touched. By default, this function is intended for converting the path section of a URL.
If you want to use your strings as query strings you can usequote_plus
:
>>>fromurllibimportparse>>>string_raw="Test: Hey! this is an example string">>>parse.quote(string_raw)'Test%3A+Hey%21+This+is+an+example+string'
quote_plus()
is likequote()
but also replace spaces with plus signs. It is required for building HTML form values when building up a query string to go into a URL.
For more info you can look at official python doc:quote,quote_plus
All done!
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse