String Slicing
M3-R5.1 · Chapter 5: Sequence Data Types · 5 min read
String Slicing: As we know slice means “part of”. So slicing is a process of extracting part of string
OR
Slicing in Python refers to extracting a portion (or "slice") of a sequence type—like a list, tuple, string, or range—using a specific syntax.
Syntax:
Str [start : stop : step]
- If step value is positive, then we move in forward direction.
- If step value is negative, then we move in backward direction.
- Missing index before colon is considered as 0
- Missing index after colon is considered as length of string
- Slice operator with strings may have third index. Which is known as step. It is optional.
Example:
|
str="welcome" for i in range (len(str)): print(i,":",str[i]) |
0 : w 1 : e 2 : l 3 : c 4 : o 5 : m 6 : e |
|
>>> str=”welcome” >>>str[2:6] Output: lcom |
>>> str=”welcome” >>>str[1:6:2] Output: ecm |
>>> str=”welcome” >>>str[1:5:2] Output: ec |
>>> str=”welcome” >>>str[:4] Output: welc |
>>> str=”welcome” >>>str[2:] Output: lcome |
|
>>> str=”welcome” >>>str[1: :2] Output: ecm |
>>> str=”welcome” >>>str[: :2] Output: wloe |
>>> str=”welcome” >>>str[6] Output: e |
>>> str=”welcome” >>>str[10] Output: index out of range |
>>> str=”welcome” >>>str[10: :] Output: “ “ |
Example:
>>>text = "Prakash, Mishra"
>>>print(text[0:5])
Praka
>>>print(text[7:])
, Mishra
>>>print(text[:5])
Praka
>>> print(text[::2])
Paah iha
Interesting Fact: Index out of bounds causes error with strings but slicing a string outside the index does not cause an error.
|
>>>str[14] IndexError: string index out of rang |
>>> str[14:20] # both indices are outside the bounds ' ' # returns empty string |
Example: Write a program to print the character with +ve and -ve Index number
|
str="welcome" j=len(str) for i in range (j): print(i,":",str[i],-j) j=j-1 |
0 : w -7 1 : e -6 2 : l -5 3 : c -4 4 : o -3 5 : m -2 6 : e -1 |
Example:
|
|
>>> str=”welcome” >>>str[-6 :] Output: elcome |
>>> str=”welcome” >>>str[-6 :-1:-2] Output: “ “ |
>>> str=”welcome” >>>str[-6 :-1:2] Output: ecm |
|
||||
|
>>> str=”wonderful” >>>str[0:6] Output: wonder |
>>> str=”wonderful” >>>str[0:3] Output: won |
>>> str=”wonderful” >>>str[3:6] Output: der |
>>> str=”wonderful” >>>str[-1:-3] Output: “” |
>>> str=”wonderful” >>>str[-3:-1] Output: fu |
||||
|
>>> str=”wonderful” >>>str[-3:0] Output: “ |
>>> str=”wonderful” >>>str[0: :2] Output: wnefl |
>>> str=”wonderful” >>>str[3:3] Output: ‘ ‘ |
>>> str=”wonderful” >>>str[3:4] Output: d |
>>> str=”wonderful” >>>str[-5:-2] Output: erf |
||||
|
>>> str=”wonderful” >>>str[:-2] Output: wonderf |
>>> str=”wonderful” >>>str[:4] Output: wond |
>>> str=”wonderful” >>>str[-3] Output: ful |
>>> str=”wonderful” >>>str[: : -1] Output: lufrednow |
|||||
NOTE: For any index position n: str1[:n] +str1[n:] will give you the original string
Syntax: str1[:n]+str1[n:] # output will be string # where n is size of string
>>>str1=”welcome”
>>> str1[:7] + str1[7:]
Welcome
NOTE: String slicing will never return error even if you pass index which is not in the string.
Example:
>>> str1[10] will give error, but
>>> str1[10:15] will not give error but return empty string
Example:
|
Forward Indexing |
S=”PYTHON PROGRAMMING” |
||||||||||||||||
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
|
P |
Y |
T |
H |
O |
N |
P |
R |
O |
G |
R |
A |
M |
M |
I |
N |
G |
|
|
-18 |
-17 |
-16 |
-15 |
-14 |
-13 |
-12 |
-11 |
-10- |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
|
Backward Indexing |
|||||||||||||||||
Example:
|
>>> s[: :-2] |
>>> s[2:3] |
>>> s[2:3:-1] |
>>> s[2:3:-1] |
>>> s[2:3:-1] |
>>> s[-6:7:-2] |
|
|
Output: GIMROPNHY |
Output: T |
Output: “ “ |
Output: “ “ |
Output: H |
Output: AGR |
|
|
>>> S[9: -10] |
>>> S[9: -10:-1] |
>>> S[22: :-6] |
>>> S[2:-22:-1] |
>>> S[7:-1] |
>>> s[4:-8] |
|
|
Output:“ “ |
Output:“O” |
Output:“GRN” |
Output:“TYP” |
Output:“PROGRAMMING” |
Output: ON PRO |
|