Python program to remove last N characters from a string Last Updated : 07 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we’ll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions.Using String SlicingString slicing is one of the simplest and most efficient ways to manipulate strings in Python. By leveraging slicing, we can easily remove the last N characters from a string with minimal code. Python s = "GeeksForGeeks" n = 3 # Remove the last N characters result = s[:-n] print(result) OutputGeeksForGe Explanation:s[:-n] slices the string from the start to the position len(s) - n, effectively removing the last N characters.If n is 0, it keeps the entire string unchanged.Let's explore various other methods :Table of ContentUsing a LoopUsing rstrip with ConditionsUsing join and List SlicingUsing a LoopUsing a loop, we can iteratively construct a new string by excluding the last N characters from the original string Python s = "GeeksForGeeks" n = 3 # Remove the last N characters result = "" for i in range(len(s) - n): result += s[i] print(result) OutputGeeksForGe Explanation:This approach iterates through the string up to len(s) - n and appends each character to a new string, omitting the last N characters.Using rstrip with ConditionsThe rstrip method, combined with conditions, allows us to remove the last N characters by treating them as a specific substring to strip. Python # Input string and N s = "GeeksForGeeks" n = 3 # Remove the last N characters result = s.rstrip(s[-n:]) if n > 0 else s print(result) OutputGeeksForG Explanation:rstrip(s[-n:]) removes the last N characters by treating them as a substring to strip.This approach is useful if the string has trailing characters that match those being removed.Using join and List SlicingBy combining list slicing with join, we can efficiently create a new string that excludes the last N characters from the original. Python s = "GeeksForGeeks" n = 3 # Remove the last N characters result = ''.join(s[:len(s) - n]) print(result) OutputGeeksForGe Explanation:This approach slices the string up to len(s) - n and then joins the resulting characters into a new string. Comment More infoAdvertise with us Next Article Get first n chars from a str column in Python Polars T thotasravya28 Follow Improve Article Tags : Python Python-string-functions Practice Tags : python Similar Reads Remove last character from the file in Python Given a text file our task is to delete the last character from the file using Python Example: Input: File before removing the last character GeeksforGeeks Output: File after removing the last character GeeksforGeekExample 1: Using the seek and truncate functionPython3 with open('geeks.txt','w') as 2 min read How to Remove Letters From a String in Python Removing letters or specific characters from a string in Python can be done in several ways. But Python strings are immutable, so removal operations cannot be performed in-place. Instead, they require creating a new string, which uses additional memory. Letâs start with a simple method to remove a s 3 min read How to reverse a String in Python Reversing a string is a common task in Python, which can be done by several methods. In this article, we discuss different approaches to reversing a string. One of the simplest and most efficient ways is by using slicing. Letâs see how it works:Using string slicingThis slicing method is one of the s 4 min read Get first n chars from a str column in Python Polars Polars is a powerful DataFrame library designed for speed and ease of use, particularly with large datasets. If you need to extract the first n characters from a string column in a Polars DataFrame, Polars offers efficient and straightforward methods to achieve this. In this article, we will go thro 3 min read How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str 4 min read Remove Last Element from List in Python Removing the last element from a list means deleting the item at the end of list. This is often done to update or shorten the list, discard unwanted data or manage memory in certain programs.Example:lnput: [1,2,3,4,5]Output: [1,2,3,4]There are many ways to remove last element from list in python, le 2 min read Like