1>>> s = "python"
2>>> s[3]
3'h'
4>>> s[6]
5Traceback (most recent call last):
6 File "<stdin>", line 1, in <module>
7IndexError: string index out of range
8>>> s[0]
9'p'
10>>> s[-1]
11'n'
12>>> s[-6]
13'p'
14>>> s[-7]
15Traceback (most recent call last):
16 File "<stdin>", line 1, in <module>
17IndexError: string index out of range
18>>>
1Random = "Whatever"#you can put anything here
2words2 = Random.split(" ")
3print('number of letters:')#you can delete this line...
4print(len(Random))#and this one. these lines just Tell you how many
5#letters there are
6while True:#you can get rid of this loop if you want
7 ask = int(input("what letter do you want?"))
8 print(Random[ask-1])
1#Accessing string characters in Python
2str = 'programiz'
3print('str = ', str)
4
5#first character
6print('str[0] = ', str[0])
7
8#last character
9print('str[-1] = ', str[-1])
10
11#slicing 2nd to 5th character
12print('str[1:5] = ', str[1:5])
13
14#slicing 6th to 2nd last character
15print('str[5:-2] = ', str[5:-2])
1>>> s = 'foobar'
2
3>>> s[0]
4'f'
5>>> s[1]
6'o'
7>>> s[3]
8'b'
9>>> len(s)
106
11>>> s[len(s)-1]
12'r'
13