-
Notifications
You must be signed in to change notification settings - Fork 180
/
stringIndexing.py
29 lines (24 loc) · 817 Bytes
/
stringIndexing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# string indexing
'''
Indexing
0 1 2 3 4
H E L L O
'''
message = 'Hello'
print(message[0]) # this will print H that is first letter in the string
print(message[1:4]) # this will print from index one to index four
print(message[:3]) # this will print from starting to index 3
print(message[2:]) # this will print from index 2 till end
print(message[:]) # this prints whole string
print(message[0:4:2]) # this escapes 2 characters from string
# negative Indexing
'''
negative Indexing
P y t h o n
-6 -5 -4 -3 -2 -1
'''
awesome = 'Python is awesome'
print(awesome[:-1]) # -1 prints last character
print(awesome[-2]) # this prints m from starting
print(awesome[-7:]) # try this one out in interpreter
print('You are ' + awesome[10:] + ' you are learning ' + awesome[:6])