TAGS :Viewed: 15 - Published at: a few seconds ago

[ chr for non-ASCII characters in Python ]

I'm trying to implement a search through a list of strings, in a context where there's no way to use something like str.startswith (If you're curious about it, I'm querying the app engine datastore.) I'd like to look for every string that has a certain prefix, let's say 'py'.

I have comparison operators at my disposal, so I was thinking I could implement this as follows.

#pseudo code
search = "py"
search_strings_where(s > search, s < chr(ord(search[0]) + 1)

The chr(ord(search[0]) + 1) is supposed to be the character that's in lexicographical order right after the first character of the search query.

The problem is that this won't work. take for example ord(u"‰"), which returns 8240. But putting that into chragain raises an error.

ValueError: chr() arg not in range(256)

How could I solve this?

EDIT Just found out about unichr, checking if this works. I will write an answer if it does.

Answer 1

Perhaps use unichr(), this function will be worked