114

Lets say I have a string that consists of x unknown chars. How could I get char nr. 13 or char nr. x-14?

7 Answers 7

167

First make sure the required number is a valid index for the string from beginning or end , then you can simply use 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
>>> 
5
  • 1
    You can pass negative integers Jan 13, 2012 at 9:30
  • @AviramSegal Thanks for the correction,Yes we can, but they should also be in the limits of the string length. Jan 13, 2012 at 9:39
  • 1
    after your edit its the best answer, voted up instead of down :) Jan 13, 2012 at 9:41
  • 1
    This answer could be improved by using a different word with unique characters at each index. As it stands s[3] is ambiguous as to which 'l' it is returning. Apr 20, 2015 at 0:20
  • 1
    Why s[-5] can work, but s[-6] would complain an index out of range error? So curious about the implementation in the string object in Python.
    – Alston
    Jun 18, 2016 at 14:52
7
In [1]: x = "anmxcjkwnekmjkldm!^%@(*)#_+@78935014712jksdfs"
In [2]: len(x)
Out[2]: 45

Now, For positive index ranges for x is from 0 to 44 (i.e. length - 1)

In [3]: x[0]
Out[3]: 'a'
In [4]: x[45]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)

/home/<ipython console> in <module>()

IndexError: string index out of range

In [5]: x[44]
Out[5]: 's'

For Negative index, index ranges from -1 to -45

In [6]: x[-1]
Out[6]: 's'
In [7]: x[-45]
Out[7]: 'a

For negative index, negative [length -1] i.e. the last valid value of positive index will give second list element as the list is read in reverse order,

In [8]: x[-44]
Out[8]: 'n'

Other, index's examples,

In [9]: x[1]
Out[9]: 'n'
In [10]: x[-9]
Out[10]: '7'
2
  • You should offer some verbal description of what's going on, even though the question may seem basic to you.
    – Hannele
    Jan 24, 2012 at 20:54
  • Updated the answer with some description, hope it helps :)
    – avasal
    Jan 25, 2012 at 3:43
6

Previous answers cover about ASCII character at a certain index.

It is a little bit troublesome to get a Unicode character at a certain index in Python 2.

E.g., with s = '한국中国にっぽん' which is <type 'str'>,

__getitem__, e.g., s[i] , does not lead you to where you desire. It will spit out semething like . (Many Unicode characters are more than 1 byte but __getitem__ in Python 2 is incremented by 1 byte.)

In this Python 2 case, you can solve the problem by decoding:

s = '한국中国にっぽん'
s = s.decode('utf-8')
for i in range(len(s)):
    print s[i]
2

Python.org has an excellent section on strings here. Scroll down to where it says "slice notation".

1

Another recommended exersice for understanding lists and indexes:

L = ['a', 'b', 'c']
for index, item in enumerate(L):
    print index + '\n' + item

0
a
1
b
2
c 
1

I think this is more clear than describing it in words

s = 'python'
print(len(s))
6
print(s[5])
'n'
print(s[len(s) - 1])
'n'
print(s[-1])
'n'
0

This should further clarify the points:

a = int(raw_input('Enter the index'))
str1 = 'Example'
leng = len(str1)
if (a < (len-1)) and (a > (-len)):
    print str1[a]
else:
    print('Index overflow')

Input 3 Output m

Input -3 Output p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.