Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
Lets say I have a string that comprises of x unknown chars. How is it possible that I would get char nr. 13 or char nr. x-14?

1 Answer

0 votes
by (26.4k points)

First ensure the necessary number is a valid index for the string from starting or end , at that point you can just utilize array subscript notation. use len(s) to get string length

>>> s = "python"

>>> s[3]

'h'

>>> s[6]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

IndexError: string index out of range

>>> s[0]

'p'

>>> s[-1]

'n'

>>> s[-6]

'p'

>>> s[-7]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

IndexError: string index out of range

>>> 

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...