Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

String Slicing in Python

In this article, we will learn to perform slicing operations on a string in Python. We will use a built-in function, a simple approach, and some custom codes as well to better understand the topic of slicing. Let's first have a quick look over what is a string and string slicing in Python.

Python String

The string is a type in python language just like integer, float, boolean, etc. Data surrounded by single quotes or double quotes are said to be a string. A string is also known as a sequence of characters. Strings are accessed using index values just like lists, sets, etc. in python.

string1 = "apple"
string2 = "Preeti125"
string3 = "12345"
string4 = "pre@12"

String Slicing

Slicing is a concept of retrieving a subset of elements from any iterable (list, set, dictionary). Here, we will perform the slicing operation on strings. Slicing of strings prints a substring. We know that, a string is immutable that means its original value cannot be changed, so slicing prints a new string from the original string.

Lets us look at two different ways to perform slicing on strings.

Example: String Slicing Using slice() Method

This method uses the built-in slice() method. It creates a slice object and represents the set of indices specified by the given range. It returns a sliced object containing elements in the given range only. The below example takes only the stop argument. The start argument has a default value equal to 0. As, index value starts from 0, it prints 0,1,2 index values.

#original string
string ='STUDYTONIGHT'
  
# Using slice()
s1 = slice(3) 

#new string  
print("String slicing- ",  string[s1]) 


String slicing- STU

Example: String Slicing Using slice() Method

It starts from the 2nd index (U), prints every 2nd value(U, Y,..) after incrementing by 2, and stops at the 10th index value (H).

#original string
string ='STUDYTONIGHT'
  
# Using slice()
s1 = slice(2, 10, 2) 

#new string  
print("String slicing- ",  string[s1]) 


String slicing- UYOI

Example: String Slicing Using slice() Method

The below example takes three negative indexes. Negative indexes are usually for traversing the string in reverse order. The negative indexes print values from right to left.

start = -1 (starts from last index)

stop = -12 (counting from 1 from right towards left, in this case, the stop index will be 0th index from left)

step = -2( goes from right to left)

#original string
string ='STUDYTONIGHT'
  
# Using slice()
s1 = slice(-1, -12, -2) 

#new string  
print("String slicing- ",  string[s1]) 


String slicing- TGNTDT

Example: String Slicing

Here, we are using square brackets and colon to slice string and get sub string of any length. See the example below.

#original string
string ='STUDYTONIGHT'

#new string  
print("String slicing- ",  string[2:10]) 


String slicing- UDYTONIG

Similarly, you can observe different outputs using different index values on strings.

Conclusion

In this article, we learned to perform string slicing by using a built-in function called slice() and also by directly applying indexes on strings to find new substrings. We used some custom examples as well to better understand the slicing property,