I'm 90% sure there is a built in function that does this.
I need to find the position of a character in an alphabet. So the character "b" is position 1 (counting from 0), etc. Does anyone know what the function is called?
Thanks in advance!
EDIT: What i'm trying to do is to send all the characters X amount of "steps" back in the alpha bet, so if i have a string with "hi" it would be "gh" if i sent it back one step. There might be a better way of doing it, any tips?
A letter's position in Alphabet can easily be found by performing logical AND operation with the number 31. Note that this is only applicable to letters and not special characters. Every letter has an ASCII value which can be represented in binary form.
int position = 'g' - 'a' + 1; In C, char values are convertible to int values and take on their ASCII values. In this case, 'a' is the same as 97 and 'g' is 103. Since the alphabet is contiguous within the ASCII character set, subtracting 'a' from your value gives its relative position.
S, or s, is the nineteenth letter in the Modern English alphabet and the ISO basic Latin alphabet.
It is called index
. For e.g.
>>> import string >>> string.lowercase.index('b') 1 >>>
Note: in Python 3, string.lowercase
has been renamed to string.ascii_lowercase
.
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