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

Hi, I’m Pramod Kumar Yadav & a computer science Engineer with 3years of experience in Digital marketing. I’m self-motivated and love to do SEO.




Best PYthon Course

Get More trending Courses

1 thought on “Python Last Character from String”

Leave a Reply

%d bloggers like this: