Slicing of a String in Python
In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is.
String
String is a group of characters that are stored under a certain variable. Python does not have a character data type. So a string with single variable is called as a character in Python. String is a data type which represented as str in Python. These strings are stored as character arrays in a variable.
We can access these strings using indexing. Highly, indexing is used because it is an easy way of accessing characters in a string.
String index positions are represented as following.
Example:
Let us a string for an example for explaining indexing in a string
S=”JOE ROOT
INDEX: 0 1 2 3 4 5 6 7
CHARACTER: J O E R O O T
LENGTH OF THE STRING IS: 8
The given string contains 7 characters and a space.
We can finally say that the way of indexing strings and the way of indexing arrays are same.
Properties:
1.) String is immutable.
The way in which the contents of the given data type can be changed is called mutability.
The strings are immutable. Immutable means the characters inside a character array cannot be changed. Let us understand this with the help of an example.
Example:
File Name: James.py
'''
This is an example program to know whether a string is mutable or immutable or not.
If string changes then it is called as mutable
If the string does not change then it is called as immutable
'''
String="JAMES ANDERSON"
print (String [6])
# let us try to change this output A with other character 'X'
# If this change is possible then the output is termed as JAMES XANDERSON
String [6] ='X'
OUTPUT
A
Traceback (most recent call last):
File "main.py", line 13, in <module>
String[6]='X'
TypeError: 'str' object does not support item assignment
So, we can prove that string is mutable and if we break the property of mutability, then we face and error of TypeError. So, String is mutable.
2. %s is the format specifier of string in Python.
Let us explain %s with the help of an example program.
Example:
String="JAMES ANDERSON"
print ("Greatest fast bowler of cricket is %s"%String)
Output:
Greatest fast bowler of cricket is JAMES ANDERSON
There is always some specialty if a data type is used very highly. Similarly, there is a specialty for Strings for it is wide usage. The specialty is slicing of the string
Slicing:
The sub-string of a string is called as slice. The process of creating slices using an operator is called slicing operator. The slice operator is used to refer sub parts of sequences. We can take a subset of original string by using [] (square brackets). The square brackets are known as delimiters of slicing operation.
We use indices (plural form of index) for slicing.
Syntax:
String_name [start : stop :step count]
Let us understand slicing with the help of an example.
Example:
'''
This is a program written to know how a slicing operator is working in Python on a string
'''
string="RAHUL DRAVID"
# to know whether the variable s is a string or not.
print(type(string))
# indexing operation in Python
print("THE string in position 5 is '"+string[5]+"'")
# length of the string
print("The length of the string is", len (string))
# slicing operation
# syntax string_name[start_index : end_index]
print("The sub string from 1st position to 7th position is:")
print(string[1:7])
# now the characters from index 1 to index 6 are printed
# syntax string_name[start_index : end_index: step count]
print("The sub string from 1st position to 7th position with step count 2 is:")
print(string[1:7:2])
# example 2
print("to print the whole string using indexing is :")
print(string[0 : len(string)])
Output:
<class 'str'>
THE string in position 5 is ' '
The length of the string is 12
The string from 1st position to 7th position is:
AHUL D
The string from 1st position to 7th position with step count 2 is:
AU
RAHUL DRAVID
Explanation:
Character | R | A | H | U | L | D | R | A | V | I | D | |
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
So as shown in the above table. This is how an index value is assigned to the given string.
So the first code written is:
syntax string_name[start_index : end_index]
string= “RAHUL DRAVID”
print (string[1:7])
The output is
AHUL D
The output is in that way because according to the concept of indexing the characters are printed in such a way that the printing starts from index 1 (which is the start index) to the index 7-1 =6 (which is the end index) . Here according to the rules of slicing the index of end value minus one is printed
So that is the reason why
AHUL D is printed
Character | A | H | U | L | D | |
Index | 1 | 2 | 3 | 4 | 5 | 6 |
syntax string_name[start_index : end_index: step count]
string= “RAHUL DRAVID”
print (string[1 : 7 : 2])
The output of the above code is:
AU
The output can be separately differentiated as ‘A’ ,‘U’, ‘ ‘
Here 3 characters A, U, A space is printed
Here the characters are printed in the way that :
Start index which is to be printed is: 1
End index which is to be printed is : 7-1=6
Step count of the slice is: 2
So according to step count and rules of slicing the indices of:
1, 1+= 3, 3+2 =5, 5+2=7 (out of slicing end value range)
So 1st , 3rd , 5th indices are printed
Characters | A | U | “ “( the value in quotations is empty space |
Index | 1 | 1+2= 3 | 3+2= 5 |
So due to this reason the output is AU
If we get a question based on indices, follow these steps:
- Print start index character
- Then add step count to start index
- Continue this process
- Stop this process when the value of index crosses the end-1 value.
There is a one more advantage in slicing. Here we can use negative indices for getting a slice or the substring of the given String.
Negative Index Slicing Representation:
To explain the negative indices in Python which is working on Python strings let us see how the below program works.
Example:
'''
This is a program written to know how a slicing operator with the help of negative indexing is working in Python on a string
'''
string="RAHUL DRAVID"
# length of the string
print("The length of the string is")
print(len(string))
# slicing of the given string using negative indices
# syntax = string_name [ start : end ]
print("The sub string from position -6 to ( -1-1)= -2 position ")
print(string[-6 : -1])
# syntax = string_name [ start : end : step count]
print("The sub string from position -6 to ( -1-1)= -2 position with step count equals to two (step count = 2) ")
print(string[-6 : -1 : 2])
# example 2
print("The given string in reverse format")
print(string[ : : -1])
Output:
The length of the string is
12
D
The sub string from position -6 to ( -1-1)= -2 position
DRAVI
The sub string from position -6 to ( -1-1)= -2 position with step count equals to two (step count
= 2) is
DAI
The given string in reverse format
DIVARD LUHAR
Explanation:
Character | R | A | H | U | L | D | R | A | V | I | D | |
Positive Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
Negative Index | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Negative slicing in Python.
This method is also similar to positive slicing.
print(string[-6])
The output is D
The reason is because the character D is present at the position (or) index -6.
print(string[-6 : -1])
The output is
DRAVI
Character | D | R | A | V | I |
Index | -6 | -5 | -4 | -3 | -2 |
The output is in that way because according to the concept of indexing the characters are printed in such a way that the printing starts from index -6 (which is the start index) to the index -1-1 =-2 (which is the end index) . Here according to the rules of slicing the index of end value minus one is printed
So the output is DRAVI
print(string[-6 : -1 : 2])
The output is
DAI
Here the characters are printed in the way that :
Start index which is to be printed is: -6
End index which is to be printed is : -1-1=-2
Step count of the slice is: 2
So according to step count and rules of slicing the indices of:
-6, -6+2= -4, -4+2 =-2, -2+2=0 (out of slicing end value range)
So -6th, -4th, -2nd indices are printed
Characters | D | A | I |
Index | -6 | -6+2=-4 | -4+2=-2 |
print( string[::-1])
Here the string is printed in reverse manner.
Start is taken as – (length of string)
End is taken as –1
This process is done internally directly by the compiler.
So the output is
DIVARD LUHAR
This is how slicing is done on a string.