Python Slice from Last Occurrence of K
Introduction
We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on Python Strings, we may occasionally run across issues because we need to execute character removing on the last instance of an element. Data-related implementations for this are possible.
Before going to the topic lets understand some important definitions
Looping
The Python language has a process known as loops that enables the repeating of specific steps, scanning, or integration of a related set of steps repeatedly predicated just on keyword that enables such features being used, and that causes steps stipulated underneath the keyword to instantly indent appropriately.
String slicing
The slice operator is a new operator that Python offers. We could split the elements as needed with the use of a slice operator. Additionally, this operator works with all Python data types, including list, bucket, string, etc. The slice() method is also available in Python. With the aid of arguments, you can tell the slice operators and slice function what range to divide into.
rfind()
The highest index—or the substring's top right occurrence—in the text sequence is returned using the Python rfind() string function. By highest index, we mean that if a particular substring appears twice or three times in a string, the rfind() method would provide the top right or last instance of the substring, which is why we state that it provides the decisive factor of the substring.
There are many possible ways to implement this slicing from the last occurrence of K. few of the methods are
- Using of max() method
- Using string slicing along with rfind() method
- Using string slicing along with looping
max() Method Usage
In the below code, we will show how to use the max method.
Code
# example python program for implementation
# of Slice from Last Occurrence of K with
# the help of looping and string slicing
# declare any example string
string = ' Kill-the-goose-that-lays-the-golden-eggs '
# Declaring the K
K = "-"
x = []
result = []
for i in range( 0, len(string)):
if(string[i] == K):
x.append(i)
result = string[:max(x)]
# displaying the initialized string
print(' Declared String is = ' + str(string))
# displaying the string after slicing
print(' String after slicing is = ' + str(result))
Output
Declared String is = Kill-the-goose-that-lays-the-golden-eggs
String after slicing is = Kill-the-goose-that-lays-the-golden-eggs
String Slicing Combined with rfind()
Such issue can be fixed by combining the aforementioned techniques. In this, we retrieve the most recent occurrence utilizing rfind() and slice the remaining data using the previous way.
Code
# example python program for implementation
# of Slice from Last Occurrence of K with
# the help of string slicing and rfind()
# declare any example string
string = ' Kill-the-goose-that-lays-the-golden-eggs '
# Declaring the K
K = " - "
# Slicing from the Last Occurrence of the K with
# the help of rfind() and string slicing
index = string.rfind( K )
result = string[: index ]
# displaying the initialized string
print(' Declared String is = ' + str(string))
# displaying the string after slicing
print(' String after slicing is = ' + str(result))
Output
Declared String is = Kill-the-goose-that-lays-the-golden-eggs
String after slicing is = Kill-the-goose-that-lays-the-golden-eggs
Combining a Loop with String Cutting
This issue can be resolved by combining the aforementioned techniques. In this, we use a loop to look for the most recent occurrence and preserve the index for subsequent slicing.
Code
# example python program for implementation
# of Slice from Last Occurrence of K with
# the help of looping and string slicing
# declare any example string
string = ' Kill-the-goose-that-lays-the-golden-eggs '
# Declaring the K
K = " - "
# Slicing from the Last Occurrence of the K with
# the help of looping and string slicing
# set index to none
index = None
# initialize the loop
for i in range(len(string)):
if K == string[ i ]:
index = i
result = string[ : index ]
# displaying the initialized string
print(' Declared String is = ' + str(string))
# displaying the string after slicing
print(' String after slicing is = ' + str(result))
Output
Declared String is = Kill-the-goose-that-lays-the-golden-eggs
String after slicing is = Kill-the-goose-that-lays-the-golden-eggs