2. By using slice operator:-
Syntax:- [start:stop:step]
st |
H |
E |
L |
L |
O |
|
W |
O |
R |
L |
D |
Postive index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
Negative index |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Note: 1. [ ] इसे slice operator के रूप में जाना जाता है इसका use किसी string के sub-string को access करने के लिए किया जाता है।
2. [:] इसे range slice operator के रूप में जाना जाता है।
*
a=”hellow world”
print(a[0:5:1]) # hello
*
a=”hellow world”
print(a[0:5]) # hello
*
a=”hellow world”
print(a[:5]) # hello
*
a=”hellow world”
print(a[5:]) # w world
*
a=”hellow world”
print(a[0::2]) # hlo ol
2. By using slice operator:-
Syntax:- [start:stop:step]
st |
H |
E |
L |
L |
O |
|
W |
O |
R |
L |
D |
Postive index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
Negative index |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Note: 1. [ ] It is known as slice operator and is used to access the sub-string of a string.
2. [:] This is known as the range slice operator.
*
a=”hellow world”
print(a[0:5:1]) # hello
*
a=”hellow world”
print(a[0:5]) # hello
*
a=”hellow world”
print(a[:5]) # hello
*
a=”hellow world”
print(a[5:]) # w world
*
a=”hellow world”
print(a[0::2]) # hlo ol