×

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

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

Python Pickle

The pickle is a module that enables serialization and de-serialization of the structure of the object in python. Pickling is the process that uses the protocols to convert the Python...

5 minutes read.

Python elif

Python elif The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax if expression1: statement elif expression2: statement elif expression3: statement else: statement The elif statement can be optional...

3 minutes read.

Conditional Expressions in Python

In Python conditional expression are sometimes referred to operator called as ternary operator. Not only Python supports ternary operator but many other programming languages supports it. Ternary operator are the...

2 minutes read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

How to Substring a String in Python

String Unicode characters collection is referred to as a String. It consists of a succession of characters, including alphanumeric and special characters. Substring: Core Concept A substring is a piece of a string....

5 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.