Python: Finding the value of a character in a string

Viewed 60851

Given a long string such as:

"fkjdlfjzgjkdsheiwqueqpwnvkasdakpp"

or

"0.53489082304804918409853091868095809846545421135498495431231"

How do I find the value of the nth character in that string? I could solve this if I could convert it into a list (where each digit gets it's own index) but since the numbers aren't separated by commas I don't know how to do this either.

AFAIK there is no command like string.index(10) which would return the 11th character in.

3 Answers

To get the corresponding character in the nth value, just use this function:

def getchar(string, n):
    return str(string)[n - 1]

Example usage:

>>> getchar('Hello World', 5)
'o'
Related