To remove the last N characters of a string in Python, you can use string slicing with the range from 0 to len(string) - N.
In this example,
str1
with a value of 'Hello World'
.str1 = 'Hello World'
modified_str = str1[:-3]
print('Modified string is:', modified_str)
Modified string is: Hello Wo
In this example,
str2
with a value of 'apple'
.str2 = 'apple'
modified_str = str2[:-2]
print('Modified string is:', modified_str)
Modified string is: app
In this tutorial, we learned How to Remove last N Characters in a String in Python language with well detailed examples.