Slicing

Python supports negative index slicing along with positive slicing. Negative index starts from -1 to -(iterable_length). We will use the negative slicing to get the elements from the end of an iterable.

The index -1 gets you the last element from the iterable. The index -2 gets you the 2nd last element from the iterable. And it continuos till the first element.

Let’s see an example. The above program will print the last and first characters from the string using negative indexing. How do we remove the last element from the string using slicing? It’s just a line of code. We know how to extract a part of the string using slicing. Let’s apply the same thing with a negative index to remove the last character from the string.

Slice the string from start to the last before element.

Let’s focus on the second line in the above code. That’s the magic line in the code. As a traditional slicing, it extracts the substring from the starting index to last but one as slicing ignores the second index element given. You will get Geekflare as output if you run the above code.

rstrip

The string method rstrip removes the characters from the right side of the string that is given to it. So, we can use it to remove the last element of the string. We don’t have to write more than a line of code to remove the last char from the string.

Give the last element to the strip method, it will return the string by removing the last character.

Let’s see the code snippet. We have given the last character of the string to the strip method. It removes the last character from the string and returns a copy without the last character. It will print Geekflare in the console, if you execute it.

Practical Example – remove the last word

Yeah, we are going to apply what we have in the previous sections in a practical example. Let’s say we have a file that contains multiple lines of text. And we need to remove the last word from each line in the file. Follow the below steps to write the program.

Create a file called random_text.txt and page a few lines of text in it. Initialize a data variable as an empty string. Open the file using with and open method in read and write mode. Read the content of the file using the readlines method. Iterate over each line of the content. Split the line of text using the split method in words. Remove the last word using one of the above methods. Join the result to form a string. Append the result to the data variable. Remove the data from the file using seek and truncate methods. Write the latest data to the file using write method.

The file contains the following data. See the code below. If you execute the above code with the given file, then the file will have the following updated data. Hope you enjoyed the tutorial. Happy coding 🙂 Also check Substring Operations in Python.

How to Remove Last Character from Python String  - 27How to Remove Last Character from Python String  - 69How to Remove Last Character from Python String  - 53How to Remove Last Character from Python String  - 80How to Remove Last Character from Python String  - 67How to Remove Last Character from Python String  - 2How to Remove Last Character from Python String  - 92How to Remove Last Character from Python String  - 25How to Remove Last Character from Python String  - 92How to Remove Last Character from Python String  - 72How to Remove Last Character from Python String  - 73How to Remove Last Character from Python String  - 74How to Remove Last Character from Python String  - 15How to Remove Last Character from Python String  - 64How to Remove Last Character from Python String  - 68How to Remove Last Character from Python String  - 66How to Remove Last Character from Python String  - 91How to Remove Last Character from Python String  - 46How to Remove Last Character from Python String  - 76How to Remove Last Character from Python String  - 42How to Remove Last Character from Python String  - 12How to Remove Last Character from Python String  - 97How to Remove Last Character from Python String  - 11