- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I get last 4 characters of a string in Python?
The slice operator in Python takes two operands. First operand is the beginning of slice. The index is counted from left by default. A negative operand starts counting from end. Second operand is the index of last character in slice. If omitted, slice goes upto end.
We want last four characters. Hence we count beginning of position from end by -4 and if we omit second operand, it will go to end.
>>> string = "Thanks. I am fine" >>> string[-4:] 'fine'
- Related Articles
- How to get last 4 characters from string in C#?
- How to get last 2 characters from string in C# using Regex?
- How can I cut a string after X characters in JavaScript?
- How can I get the color of the last figure in Matplotlib?
- How to get first 100 characters of the string in Python?
- How can we convert a list of characters into a string in Python?
- How can we get some last number of characters from the data stored in a MySQL table’s column?
- MySQL query to get a substring from a string except the last three characters?
- How to extract the last 4 characters from NSString?
- How can I eliminate numbers in a string in Python?
- How can we get substring from a string in Python?
- How can I get a list of locally installed Python modules?
- How to get last day of a month in Python?
- How to extract initial, last, or middle characters from a string in R?
- How can I convert bytes to a Python string?

Advertisements