简体   繁体   中英

How do I urlencode all the characters in a string, including safe characters?

Using Python, how would I encode all the characters of a string to a URL-encoded string?

As of now, just about every answer eventually references the same methods, such as urllib.parse.quote() or urllib.parse.urlencode() . While these answers are technically valid (they follow the required specifications for encoding special characters in URLs), I have not managed to find a single answer that describes how to encode other/non-special characters as well (such as lowercase or uppercase letters).

How do I take a string and encode every character into a URL-encoded string?

This gist reveals a very nice answer to this problem. The final function code is as follows:

def encode_all(string):
    return "".join("%{0:0>2}".format(format(ord(char), "x")) for char in string)

Let's break this down.

The first thing to notice is that the return value is a generator expression ( ... for char in string ) wrapped in a str.join call ( "".join(...) ). This means we will be performing an operation for each character in the string, then finally joining each outputted string together (with the empty string, "" ).

The operation performed on each character in the string is "%{0:0>2}".format(format(ord(char), "x")) . This can be broken down into the following:

  • ord(char) : Convert each character to the corresponding number.
  • format(..., "x") : Convert the number to a hexadecimal value.
  • "%{0:0>2}".format(...) : Format the hexadecimal value into a string (with a prefixed "%" ).

When you look at the whole function from an overview, it is converting each character to a number, converting that number to hexadecimal, then jamming all the hexadecimal values into a string (which is then returned).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM