Can we increase the lowercase character in one

if I have

char = 'a'

how can i increase the value in 'b' and then in 'c' etc.

I do not want to replace or change it. Its very similar to

char = char + 1
+5
source share
4 answers
>>> chr(ord('a') + 1)
'b'
+10
source

You can make such a translation. I mapped 'z' back to 'a' in this case

>>> from string import maketrans, ascii_lowercase
>>> char_incrementer = maketrans(ascii_lowercase, ascii_lowercase[1:]+ascii_lowercase[0])
>>> 'a'.translate(char_incrementer)
'b'

you can just as easily apply it to an entire line

>>> 'hello'.translate(char_incrementer)
'ifmmp'
+8
source

:

char = chr(ord(char) + 1)

, , pythonic:

from string import ascii_lowercase
char = ascii_lowercase[ascii_lowercase.index(char) + 1]

, z.

, , , , , , , . , :

char = "a"
while True:
    if xxx():
        break
    if yyy():
        continue
    value = zzz()
    print char, value
    char = chr(ord(char) + 1)

:

def find_values():
    while True:
        if xxx():
            break
        if yyy():
            continue
        yield zzz()

for char, value in zip(ascii_lowercase, find_values()):
    print char, value
+4
>>> chr((ord('a')+1)%97%26 + 97)
'b'
>>> chr((ord('z')+1)%97%26 + 97)
'a'
+1

All Articles