forked from Akuli/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Slice.py
More file actions
109 lines (69 loc) · 2.01 KB
/
String_Slice.py
File metadata and controls
109 lines (69 loc) · 2.01 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
String data type:
1. Any sequence of charcaters we can call it as string
2. We can mention this data type in "Single quote / Double quotes / Trple quotes"
3. here we can use triple quotes when we are writing Multy line literals
example:
name = "Raja
gopal
Naidu"
print(name) #Error
Name = '''Raja
gopal
Naidu''' #Success
How to access characters in the string:
1. By using Indexing
2. By using Slice operater
s = 'raja'
print(s[2])
print(s[-2])
print(len(s))
Ex: Write a program to read string from the key board and display
its characters by index wise (both positive and negative index wise)
##Positive index
s = 'raja'
i=0
for x in s:
print('The chracter of positive index {} is {}' .format(i,x))
i=i+1
## Negative index
s = 'raja'
i=-1
for x in s:
print('The chracter of positive index {} is {}' .format(i,x))
i=i-1
## or if we need both indexes in one line
s = 'raja'
i=0
for x in s:
print('The chracter of positive index {} and negative index {} is {}' .format(i,i-len(s),x))
i=i+1
##another example:
s = 'DEVARA'
i=0
for x in s:
print('The chracter of positive index {} and negative index {} is {}' .format(i,i-len(s),x))
i=i+1
s = "RAMAMMM RAGAVAM"
i=0
for x in s:
print("Positive index is {} Negative index is {} letter is{}" .format(i,i-len(s),x))
i=i+1
s = "RAMAMMM RAGAVAM"
i=0
for x in s:
print(x)
i=i+1
print(i)
#### Indexing by using Slice operater:
Slice operater syntax = [Begain value : End value : Step value]
slice operater won't rise index error
string = "0123456789012345"
print(string[2:100])
print(string[3:7])
Slice [Begin value : End value : Step value ]
All atributes are optional in slicing
All values can be either +ve or -ValueError
if step value is +ve then forword direction from Beagin to End-1
if step value is -ve then Backward direction from End to Beagin +1
In forward direction if End value is "0" the result is always Emply string
In the Backward direction if the end value is -1 then result is always Empty string