In Python, you can use indexing with the length of the string minus one to get the last character.
In this example,
str
with a value of "Hello World"
.[]
with str
and len(str) - 1
to get the last character.str = "Hello World"
last_char = str[len(str) - 1]
print(f'Last character of the string is: {last_char}')
Last character of the string is: d
In this example,
str
with a value of "apple"
.[]
with str
and len(str) - 1
to get the last character.str = "apple"
last_char = str[len(str) - 1]
print(f'Last character of the string is: {last_char}')
Last character of the string is: e
In this tutorial, we learned How to get Last Character in String in Python language with well detailed examples.