×

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:

CharacterRAHUL DRAVID
Index01234567891011

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

CharacterAHUL D
Index123456

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

CharactersAU“  “( the value in quotations is empty space
Index11+2=  33+2=  5

So due to this reason the output is AU

If we get a question based on indices, follow these steps:

  1. Print start index character
  2. Then add step count to start index
  3. Continue this process
  4. 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:

CharacterRAHUL DRAVID
Positive Index01234567891011
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

CharacterDRAVI
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

CharactersDAI
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.


Related Topics

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph --><p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the...

9 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

Add a key-value pair to dictionary in Python

In programming, data type defines the type of value that a variable can hold. With help of these, we can perform various mathematical, logical, or relational operations on that particular...

5 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Python vs HTML

Python and HTML are not comparable since they are two separate categories of programming languages. Building the structures and layouts of a web page or app requires the usage of HTML,...

8 minutes read.

Python Break Statement

In Python, loops are used to automate and repeat processes in an effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration,...

2 minutes read.

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

List in Python

What is List in Python In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as...

3 minutes read.

ModuleNotFoundError No module named 'mysql' in Python

mysql module not found is an error that occurs in python. It appears like ModuleNotError: No module named 'mysql.' This error occurs when you forget to install a module called...

3 minutes read.