How to delete last character from string?
Delete Last Character from String
Given a string myStr
, we need a string created from the string myStr
but without the last character.
To delete the last character from a string in Python, we can use string slicing technique. Specify the ending position as -1
, and do not specify the starting position. The slice operation must return the string without the last character.
Syntax
The syntax of the expression to get the string myStr
without last character using slicing is
myStr[:-1]
Example
In the following program, we take a string in myStr
and delete the last character.
Python Program
myStr = 'apple'
output = myStr[:-1]
print(f'Input : {myStr}')
print(f'Output : {output}')
Output
Input : apple
Output : appl
Summary
In this tutorial of Python Examples, we learned how to remove the last character from the string using slicing technique, with the help of well detailed examples.