Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

Python: Get the Last Character of String

Here, we will develop a Python program to get the last character of a string. If the string was “Knowprogram” then print the last character “m”. We will discuss how to get the last character from the given string using [] operator, and slice operator. Also, we will develop a Python program to get the last two characters of a string.

Last Character of String in Python

In python, String provides an [] operator to access any character in the string by index position. We need to pass the index position in the square brackets, and it will return the character at that index. As negative indexing of characters in a string starts from -1, So to get the last character of the given string pass the index position -1 in the [] operator i.e.

``````# Python Program get last character of string

# take input
string = input('Enter any string: ')

# get last character
last_char = string[-1]

# printing last character of string
print('Last character:', last_char)``````

Output for the different input values:-

Enter any string: Python
Last character: n

Enter any string: Know Program
Last character: m

Python Program to Find Last Character in String

In the previous program, we used negative indexing to get the last character of the string but in this program, we are using len() function. Calculated the length of the string using len() function to access the last character of the string. If we don’t want to use the negative indexing, then we can still access the last character of the string by calculating its length first and then access the character at length -1.

``````# Python Program get last character of string

# take input
string = input('Enter any string: ')

# length of string
length = len(string)

# get last character
last_char = string[length-1]

# printing last character of string
print('Last character:', last_char)``````

Output:-

Enter any string: length
Last character: h

Python Find Last Occurrence in String

We will get the last character of the string using the slice operator. The [-1:] specifies the character at index -1. The string[-1:] specifies the last characters of the given string.

``````# Python Program get last character of string

# take input
string = input('Enter any string: ')

# get last character using slicing
last_char = string[-1:]

# printing last character of string
print('Last character:', last_char)``````

Output:-

Enter any string: slicing
Last character: g

Get Last Two Characters of String in Python

In the above program, we will discuss how to get the last character of the string but in this program, we will discuss how to get the last two characters of the given string.

``````# Python Program get last two character of string

# take input
string = input('Enter any string: ')

# get last two character using slicing
last_two = string[-2:]

# printing last two character of string
print('Last character:', last_two)``````

Output:-

Enter any string: Two
Last character: wo

Get Last Letter of String in Python

This python program is different from the above program, in this program, we will print all the last characters of the given string. If the string was “Know Program” then print all last characters “wm”.

``````# Python Program get last character from a string

# take input
string = input('Enter any string: ')

# get last character
last_char = ''.join([s[-1:] for s in string.split(' ')])

# printing last character of string
print('Last character:', last_char)``````

Output for the different input values:-

Enter any string: Know Program
Last character: wm

Enter any string: last character of a string
Last character: trfag

Also See:- Extract Numbers From String Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!