Python Last Character from String

How do I Get Last Character of String in Python

There are different ways to find the last character of a string in python. In this example, we will see one of the simple examples. The last character of the string has index position -1. So to get the last character of the string we will pall -1 in the square bracket.

Syntax:

string_variable[-1]

Print Last Character of String Example:

#eg name=”Pramod” 
#Pramod is a string and in this string, there are 6 characters ‘P’,’r’,’a’,’m’,’o’,’d’ 
#now we have to print the last character that is “d” 
name = 'pramod'
print('The last character is: ',name[-1])
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
The last character is: d

Process finished with exit code 0

Source Code:

def name(first_name):

return first_name[-1]

first_name=input("enter your first name: ")

print('Last character is:' ,name(first_name))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter your first name: KUMAR
Last character is: R

Process finished with exit code 0
Get Last Character of String in Python
 

Learn Complete Python In Simple Way

Click on enroll Button and watch the sample video then Go for Paid if you like.

100 Days of Code – The Complete Python Pro Bootcamp for 2021

Click on enroll Button and watch the sample video then Go for Paid if you like.

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



1 thought on “Python Last Character from String”

Leave a Comment