-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringSlicing.py
18 lines (17 loc) · 916 Bytes
/
StringSlicing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# String Slicing means getting the sub-part/ portion of a string.
str = "De Royace"
#Index: 012345678
# -9, -8, -7, -6, -5, -4, -3, -2, -1 => Negative indexes [ -1 starts from 'e' -> pos 8]
print(str[3])
print(str[3 : 6]) # printing string characters from index 3 to 5
print(str[6:]) # prints all characters from index 6 to end
print(str[:6]) # prints all characters before index 6 [index: 0 to 5]
print(str[-6:]) # prints characters from index (-ve) 6 [ index: -6 to -1]
print()
# now we see how to Skip value/ character(s) while Slicing the string:
str = "Switzerland"
print(str[0:11:1]) # we can't see any change
# [start_index : end_index : N-valuestoskip]
# so the value we give for skip string characters asct as N-1 characters skipped
print(str[0:11:2]) # N = 2; so we are skipping 1 character while slicing the string
print(str[0::3]) # slicing from 0 to end; here N =2; so we are skipping 2 characters.