Like this:

>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'

This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:

>>> mystr[:-4]
'abcdefgh'

For more information on slicing see this Stack Overflow answer.

Answer from Constantinius on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-get-last-n-characters-of-a-string
Get Last N characters of a string - Python - GeeksforGeeks
July 23, 2025 - String slicing is the fastest and most straightforward way to get the last N characters from a string. Itโ€™s built into Python and highly optimized for such tasks.
Discussions

How to extract last three characters of a string?
echo "D:\\nim\\nim.zip"[^4..^1] More on reddit.com
๐ŸŒ r/nim
9
11
December 14, 2022
how to get what index position the last character of a string is
this could have been solved with 5 minutes of experimentation if you're going to be a programmer you have to start experimenting with code, just test and try stuff out More on reddit.com
๐ŸŒ r/learnpython
13
3
April 2, 2022
The last four characters of string
Dear all, I need to get the last four characters of few excel lines. They all have different length, but I just do need the last four. How can I do it with substring method? Many thanks :slight_smile: Angel More on forum.uipath.com
๐ŸŒ forum.uipath.com
2
0
April 22, 2021
Python- get last 2 characters of a string - Stack Overflow
I am relatively new in Python. I wanted to know if is there any possibility for me to get only the E: from the string below: p="\\.\E:" More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-can-i-get-last-4-characters-of-a-string-in-python
How can I get last 4 characters of a string in Python?
Use string[-4:] to get the last 4 characters efficiently. This method handles short strings gracefully and is the most Pythonic approach for extracting characters from the end of a string.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-get-last-four-characters-of-a-string-in-python
How to get last four characters of a string in python - Quora
Answer (1 of 6): str = "Hello How are you?" print(str[len(str)-4:]) String can be treated as an array. So we can access each char with indexing. To get last four elements simply calculate itโ€™s length using โ€œlen(str)โ€ (itโ€™s an inbuilt function in python) then on subtracting 4 from it ...
Find elsewhere
๐ŸŒ
Jeremy Morgan
jeremymorgan.com โ€บ python โ€บ how-to-get-the-last-character-of-a-string-python
How to Get the Last Character of a String in Python
The method returns the index of the last occurrence, which is 4 in this case. We can then use this index to get the last character of the string. In conclusion, there are several ways to get the last character of a string in Python, including basic slicing and more advanced techniques such as using the split() method or the rfind() method.
๐ŸŒ
RS Blog
reneshbedre.com โ€บ blog โ€บ last-characters-string.html
3 ways to get the last characters of a string in Python
August 17, 2021 - The numeric string index in Python is zero-based i.e., the first character of the string starts with 0. If you know the length of the string, you can easily get the last character of the string
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-substring-how-to-slice-a-string
Python Substring โ€“ How to Slice a String
July 27, 2021 - To get the last character use the -1 index (negative index). Check out the following example: ... The output will be โ€˜pโ€™. In this example, you will slice the last 4 characters from the string.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-string.html
Python Strings
... Each character in a string is drawn from the unicode character set, which includes the "characters" or pretty much every language on earth, plus many emojis. See the unicode section below for more information. The len() function returns the length of a string, the number of chars in it.
๐ŸŒ
Reddit
reddit.com โ€บ r/nim โ€บ how to extract last three characters of a string?
r/nim on Reddit: How to extract last three characters of a string?
December 14, 2022 -

I can't for the life of me find any documentation on how to do this so I apologize.

In python its

string = "D:\\nim\\nim.zip"
print(string[-4:])

This outputs .zip. I can see in Nim how to slice a string from the start (0 .. ^4) but not backwards. Is this possible without writing a huge function?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to get what index position the last character of a string is
r/learnpython on Reddit: how to get what index position the last character of a string is
April 2, 2022 -

Hey. I'm trying to get the index position of the last character in a string. For example, if the string is "Hello" , I want the program to only print that the last character (in this case 'o') is in index position 4. The string would be inputted by the user though so its always going to be different.

I'm familiar with (len(string)) and string[-1] but not sure how to use them together, if I even need to.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default. step: Every "step" character after the current character to be included. The default value is 1. If step is not included, it is assumed to be equal to 1. string[start:end]: Get all characters from start to end - 1
๐ŸŒ
UiPath Community
forum.uipath.com โ€บ help โ€บ activities
The last four characters of string - Activities - UiPath Community Forum
April 22, 2021 - Dear all, I need to get the last four characters of few excel lines. They all have different length, but I just do need the last four. How can I do it with substring method? Many thanks ๐Ÿ™‚ Angel
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-get-the-last-n-characters-of-a-string-in-python
How to Get the Last N characters of a String in Python
July 18, 2023 - def get_last_n_characters(text, n): slice_obj = slice(-n, None) return text[slice_obj] text = "Hello, world!" n = 5 result = get_last_n_characters(text, n) print(f"Last {n} characters: {result}") # Reusing the slice object last_3 = slice(-3, None) print("Python"[last_3]) # hon print("Programming"[last_3]) # ing ... The deque with maxlen parameter automatically maintains only the last N elements, making it memory-efficient for large strings.
Top answer
1 of 1
9

Sure you can do this using slicing.

p="\\.\E:"
result = p[-2:]  # Operates like: p[len(p) - 2 : len(p)]
# Where the colon (:) operator works as a delimiter that
# represent an "until" range. If the left hand starting
# value is missing it assumes a default 
# starting position of 0, if the right hand end position 
# value is missing it assumes the default to be the length 
# of the string operated upon. If a negative value is 
# passed to either the left or right hand value, it 
# assumes the length of the string - the value. 
print("result: " + result)
>> result: E:

Let's look at what happens above:

We slice the string p at position -2, this is because we want a generic approach that will always start stripping from the last two characters, this without knowing how many characters in length the string is.

If we knew it's always going to be a fix number of characters in length, say 4 characters then we could do:

result = p[2:]  # Every character from 2 and on wards
# Works like: p[2:len(p)] 

This would mean, result should contain every character that comes after index 2, this is because we do not specify a max value after the colon operator :, if we wanted to, for our previous example, we could do:

result = p[2:4]  # Every character from position 2 until 4

Thus, if one were to instead want the first two characters, one could do:

result = p[:2]  # Every character until position 2

Or:

result = p[0:2]  # Every character from 0 until 2

You might also want to check out: https://www.w3schools.com/python/gloss_python_string_slice.asp

๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Get the Last 4 Characters of a String in Python ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
๐ŸŒ
Google Cloud
googlecloudcommunity.com โ€บ looker โ€บ tips & resources โ€บ technical tips & tricks
How do I get the last n characters of a string in table calculations? - Technical Tips & Tricks - Google Developer forums
June 15, 2021 - Let n be the number of characters at the end you want to get: substring(${field_name}, length(${field_name}) - (n-1), n) If I want to get the last 4 digits of the license key for example: substring(${license_key}, length(${license_key}) - 3, 4) This ...