How to get last character of String in Python?


Get Last Character of String

To get the last character of the given string in Python, we can use string indexing using square brackets.

Syntax

The syntax to get the last character of the string x is

x[len(x) - 1]

Example

In the following program, we take a string value in variable name, and get the last character of the string.

Python Program

name = 'apple'

if len(name) > 0:
    lastChar = name[len(name) - 1]
    print(f'Last character : {lastChar}')
else:
    print('The string is empty. Please check.')

Output

Last character : e

Summary

In this tutorial of Python Examples, we learned how to get the last character of a string, with examples.