>>> N = 3 >>> st[:-N] 'abcdefg'str = "string" str = str[:-1] # Returns "strin"str = "string_example" str = str[:-1] # -> returns "string_exampl" (-> without the "e")# Python program to remove last character from a string using slice notation text= 'Hello World!' print(text[:-1])string = "Hello World" string = string[:-1] # This overwrite the string to have the last letter removed. print(string)# Then we print the variable string#The rstrip() method removes any trailing characters (characters at the end #a string), space is the default trailing character to remove. string = 'Bonjour!!!!!!!!!' string.rstrip("!") #Output: 'Bonjour'your_string = "hello" your_string = your_string[:-1] # this removes the last character from your string #Removing last three characters foo = foo[:-3]''' ================================================================== How to Remove the Last Component of a String How to Move Back Up a Hierarchy with a Given Pathway/File Address ================================================================== Source: https://stackoverflow.com/questions/3315045/remove-last-path-component-in-a-string Note: If you are wanting to remove the last component of a string but it has forward slashes rather than backslashes, you will want to use ".normpath" instead of ".dirname" ''' import os.path #Asks user for desired file pathway inpDirectoryPath = input("What is your directory path? Enter here: ") print("\n\nYour given pathfile is " + inpDirectoryPath + ". Now we will delete the last component of it.") #this removes the last component of the given pathway. deletedPathComponent = os.path.dirname(inpDirectoryPath) print("\n\nThe path now looks like this: " + deletedPathComponent) ''' Output: What is your directory path? Enter here: C:\Users\Justin\Documents\Homework\Calculus Your given pathfile is: C:\Users\Justin\Documents\Homework\Calculus. Now we will delete the last component of it. The path now looks like this: C:\Users\Justin\Documents\Homework '''foo = foo[:-n]