Table of Contents
Python Slicing will consist of getting a sub-string from the provided string by slicing it accordingly. Let us check out this article to learn more about Python Slice.
What is a Slicing String?
The slicing string will allow us to return the range of characters with the slice syntax. The slicing of the string will be done using the slice operator. Thus, this operator will enable us to mention where to start and end the string.
How String Slicing in Python Works?
To learn to slice in Python, Mostly, 2 methods are used for string slicing. The first method is the in-build slice() method. Another method is referred to as the array (:) slice.
What are the methods for Python Slicing?
Python slicing is done in two ways:
- Using the slice() method.
- Using the array slicing [::] method
Method 1- Using the Slice Method
The slice constructor will help us to make the slice object that will represent the set of indices that is specified by range.
Syntax
slice(stop)
slice(start, stop, step)
Example
# String slicing in Python
text = "Hello, World!"
# Slice from index 0 to 5 (exclusive)
slice1 = text[0:5]
print("Slice 1:", slice1)
# Slice from index 7 to the end
slice2 = text[7:]
print("Slice 2:", slice2)
# Slice every second character
slice3 = text[::2]
print("Slice 3:", slice3)
Output
Slice 1: Hello
Slice 2: World!
Slice 3: Hlo ol!
Method 2: Using the List/array with the slicing[::] method
Indexing syntax in Python will allow us to use the substitute for the slice object. Hence, this refers to an easy and convenient way to slice the string with the list slicing and the array slicing using syntax and execution.
The start, end, and step are the same mechanism using the slice() constructor.
Syntax of this method
arr[start:stop] # items start through stop-1
arr[start:] # items start through the rest of the array
arr[:stop] # items from the beginning through stop-1
arr[:] # a copy of the whole array
arr[start:stop:step] # start through not past stop, by step
Example 1:
The list slicing in Python is given below:
# List slicing in Python
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Slice from index 2 to 7 (exclusive)
slice1 = my_list[2:7]
print("Slice 1:", slice1)
# Slice from index 3 to the end
slice2 = my_list[3:]
print("Slice 2:", slice2)
# Slice every second element
slice3 = my_list[::2]
print("Slice 3:", slice3)
Output
Slice 1: [3, 4, 5, 6, 7]
Slice 2: [4, 5, 6, 7, 8, 9, 10]
Slice 3: [1, 3, 5, 7, 9]
Example 2:
The example below shows the Python slicing string by character:
# Python program to demonstrate string slicing
# String slicing
string_value = 'skillvertex'
# Using different slicing
# Start from index 2, end at index 10 (exclusive), and take every 3rd character.
new_slice = string_value[2:10:3]
# Output the result
print(new_slice)
Output
itle
Example 3:
The slicing starts from the last character (‘x’) using the index -1, goes up to the third character (exclusive) at index -12 (stops at 3-1=2), and takes every second character with a step of -2.
# Python program to demonstrate string slicing
# String slicing
string_value = 'skillvertex'
# Using different slicing with negative indices
# Start from the last character (-1 index), end at the third character (-12 index, stops at 3-1=2), and take every second character with a step of -2.
new_slice = string_value[-1:-12:-2]
# Output the result
print(new_slice)
Output
xtcvi
Example 4:
The example illustrates that the whole string is printed in the reverse order.
# Python program to print the whole string in reverse order
# String to be reversed
string_value = 'skillvertex'
# Using string slicing to reverse the string
reversed_string = string_value[::-1]
# Output the reversed string
print(reversed_string)
Output
xetreversylliks
What is isslice() in Python
The isslice () is referred to as the built-in function which has been defined in the itertools module. This will allow us to get the iterator that is an index-based slicing of an iterable. This works similarly to the standard slice and thus returns the iterator.
Syntax:
itertools.islice(iterable, start, stop[, step])
Parameters: iterable: Any iterable sequence like list, string, tuple etc. start:
Example
from itertools import islice
# Original iterable
original_iterable = range(10)
# Using islice to get elements from index 2 to 7 (exclusive)
sliced_iterable = islice(original_iterable, 2, 7)
# Output the result
print(list(sliced_iterable))
Output
[2, 3, 4, 5, 6]
Conclusion
To conclude, Slicing the string will allow us to access the range of characters. This has also included several articles for a better understanding.
Python Slice String-FAQs
Q1. What is the syntax of slice in Python?
Ans. slice syntax is: start:stop: step.
Q2.What is list slicing in Python?
Ans. List slicing is like cutting out a part of a list to use it separately without changing the original list.
Q3.What is slicing in programming?
Ans. Program slicing is like looking at a big computer program and picking out only the parts of the code that directly influence the values we’re interested in. It’s a way to focus on just the lines that matter for a specific outcome.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together