I'm working on a project need this functionality very frequently
'b' + 1 #=> 'a' and 'b' - 1 #=> 'a'
Now my solution is very tedious :
str(unichr((ord('b')+ 1)))
is there a more elegant way to do this?
str(unichr(c))
can be replaced with just chr(c)
.
Simplified version:
chr(ord('b') + 1)
define your own function:
In [103]: def func(c,n):
return chr(ord(c)+n)
.....:
In [105]: func('a',-1)
Out[105]: '`'
In [106]: func('b',-1)
Out[106]: 'a'
In [107]: func('c',2)
Out[107]: 'e'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With