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:
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
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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers
1 thought on “Python Last Character from String”