Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Serhat Teker
Serhat Teker

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'
Enter fullscreen modeExit fullscreen mode

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'
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Uomo Universale | Software Engineer | Entrepreneur | builds systems | py:go:js |
  • Location
    127.0.0.1
  • Work
    Software Engineer
  • Joined

More fromSerhat Teker

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp