Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we increase a lowercase character by one

Tags:

python

If I am having

char = 'a'

how can I increase the value into 'b' and then into 'c' and so on..

I don't want to replace it or change it. Its much like

char = char + 1
like image 985
smazon09 Avatar asked Aug 06 '12 11:08

smazon09


2 Answers

>>> chr(ord('a') + 1)
'b'
like image 168
Igor Chubin Avatar answered Oct 12 '22 22:10

Igor Chubin


You can make an incrementer translation like this. I've mapped 'z' back onto '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 a whole string

>>> 'hello'.translate(char_incrementer)
'ifmmp'
like image 26
John La Rooy Avatar answered Oct 12 '22 23:10

John La Rooy