How to URL-encode text on a Mac

Percent-encode text for a URL, or decode it back, on a Mac: a one-line Python command in Terminal, or URL encode and decode in the MacBook notch.

By Rudra Satani, maker of NotchMind ·

URLs can’t contain spaces, &, ? or many other characters inside a value. URL encoding, also called percent-encoding, replaces them: a space becomes %20, & becomes %26.

In Terminal

macOS has no urlencode command, but Python does it in one line:

python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1], safe=""))' 'name=Jane Doe&city=São Paulo'

Decode:

python3 -c 'import sys, urllib.parse; print(urllib.parse.unquote(sys.argv[1]))' 'S%C3%A3o%20Paulo'

safe="" encodes / too, which you want for a single value. Leave it out to keep slashes in a path.

From the notch

NotchMind, which I make, has URL encode and URL decode on its Developer Tools page. Paste, press the arrow and copy. It handles UTF-8, so accented letters come out right. See developer tools in the notch.

Encode the value, not the whole URL

Encode each value you put after =, not the full address, or https:// turns into https%3A%2F%2F.

About the author

Rudra Satani makes NotchMind, a notch app for Mac, so read anything about notch apps here with that in mind. Spotted a mistake? Email support@notchmind.com, or reach out at @rudraxx13.