Getting the Last Character of a String in Python

In this article, we will explore how to get the last character of a string in Python. We will cover the basics of slicing and then dive into more advanced techniques for working with strings.

Author: Jeremy Morgan
Published: December 11, 2023


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.


In Python, there are several ways to get the last character of a string. The most common method is by using slicing, which allows you to extract a portion of a string based on a start and end index. In this article, we will explore how to use slicing to get the last character of a string.

Basic Slicing

The basic syntax for slicing in Python is string[start:end].

  • start: The index of the first character to include (inclusive). Defaults to 0.
  • end: The index of the last character to include (exclusive). Defaults to the string length.
  • step: The number of characters to skip between each included character. Defaults to 1.

This returns a new string that starts at the specified start index and ends at the specified end index.

For example, if we have the following string:

my_string = "hello"

We can use slicing to get the last character of the string like this:

last_char = my_string[-1]
print(last_char)

Here’s the output:

“How to get the last character of a string in Python”

In this example, we use a negative index to start at the end of the string and then specify that we want to stop at the last character. This returns the last character of the string, which is “o”.

Advanced Slicing

Here’s another way we can use slicing to extract the last letter of a string in Python. We can use slicing with a step size of -1 to get the last character of a string.

string = "hello"
print(string[::-1][0])

Here’s the output:

“How to get the last character of a string in Python”

Reverse Method

Since Python strings are immutable we can’t reverse the string in place like we can with other languages. However, you can convert the string into a list, reverse the list then join it back into a string.

for example:

string = "hello"
reversed_string = ''.join(reversed(string))
print(reversed_string)
print(reversed_string[0])

Here’s the output:

“How to get the last character of a string in Python”

Using the len() function and string indexing

Another approach is to combine the len() function and string indexing. The len() function returns the length of the string, which you can use to calculate the index of the last character.

my_string = "Hello, world!"
string_length = len(my_string)
last_index = string_length - 1
last_character = my_string[last_index]
print(last_character)

Here’s the output:

“How to get the last character of a string in Python”

This code snippet first calculates the length of the string and then subtracts 1 to get the index of the last character. Finally, it uses that index to access the last character using string indexing.

Using rfind()

Another technique you can use to get the last character of a string is to use the rfind() method. This method returns the index of the last occurrence of a specified substring in a string. For example:

my_string = "hello"
last_index = my_string.rfind("o")
print(last_index)

Here’s the output:

“How to get the last character of a string in Python”

In this example, we use the rfind() method to find the last occurrence of the substring “o” in the string. The method returns the index of the last occurrence, which is 4 in this case. We can then use this index to get the last character of the string.

Conclusion

In conclusion, there are several ways to get the last character of a string in Python, including basic slicing and more advanced techniques such as using the split() method or the rfind() method. By understanding these different methods and when to use them, you can become a more proficient Python developer and write more efficient code.


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.