Python Forum

Full Version: the next higher character
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i have a character as a string of length 1. i want the next higher character. 'a'->'b' or '4'->'5' or '9'->':'. i could do chr(ord(ch)+1). does python have any more elegant way?
#!/usr/bin/python3
import string

for ch in string.ascii_letters:
    print(ch)
what character does that code yield?
You can use a bytearray of length 1, which is a writable array containing a single integer in the range [0, 256). You can use the latin-1 encoding to convert such an array to / from a str instance of length 1 (this is the same as the utf8 encoding for ords below 128).
>>> t = bytearray('a', encoding='latin-1')
>>> t
bytearray(b'a')
>>> t[0] += 5
>>> t
bytearray(b'f')
>>> t.decode('latin-1')
'f'
Great.
(Jun-07-2019, 02:33 AM)heiner55 Wrote: [ -> ]See:
https://docs.python.org/3.7/library/string.html

i think you didn't understand my question. i do not see an answer in that link.
Because your question are always very complex.
if the question is too hard to understand why post any answer at all?
I did not think my answer was so bad.

#!/usr/bin/python3
import string
 
for ch in string.ascii_letters:
    print(ch)
Pages: 1 2