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

python - Extract the last n characters of a string in a list - Stack Overflow
I have a list of links in which some have page numbers and some don't. I'm trying to scrape the website to get the page number but it is in this format: '\n\n\n\n\n\n\n\n«\nPrevious\n\n\n\n\n\n\n1\... More on stackoverflow.com
🌐 stackoverflow.com
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
shell script - Print last N characters from all lines in a file using cut - Unix & Linux Stack Exchange
I have following content in a file foobar bar bar foo foo bar bar foobar I would like to get get the last character from all the lines, also last 3 characters. I am using following for loop worka... More on unix.stackexchange.com
🌐 unix.stackexchange.com
November 4, 2019
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
🌐
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?

🌐
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
March 27, 2026 - I am testing this message" n = 11 result = get_last_n_characters(text, n) print(f"Last {n} characters: {result}") ... def safe_get_last_n(text, n): """Handle edge cases like empty strings or n larger than string length""" if not text or n <= ...
🌐
freeCodeCamp
freecodecamp.org › news › python-substring-how-to-slice-a-string
Python Substring – How to Slice a String
July 27, 2021 - ... Here you define the stop index ... to index 5 from the string. ... The output is 'lo’. To get the last character use the -1 index (negative ......
🌐
C# Corner
c-sharpcorner.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 27, 2023 - In this article, you will learn about 3 different ways of printing the last N character from a String in Python.
Find elsewhere
🌐
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 ...
🌐
Flexiple
flexiple.com › python › last-character-string-python
Get the Last Character of a String in Python - Flexiple
April 2, 2024 - To get the last character of a ... a string, including individual characters. To get the last character, you can use the slice [-1:], which includes the last character up to the end of the string....
🌐
Reactgo
reactgo.com › home › how to get last n characters of a string in python
How to get last n characters of a string in Python | Reactgo
August 6, 2023 - str = "abcdef" lastFour = str[-4:] # getting the last 4 characters print(lastFour) ... You can also read, how to get first n characters of a string in Python.
🌐
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.
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

🌐
Thequantizer
thequantizer.com › the quantizer › python
Python: How to Get the Last Character in a String :: The Quantizer
There are many ways to get the last character of a string in a Python program. To understand all the following examples we must first understand how indexing in Python works. In Python, you can use the square brackets with a number or a slice notation to tell Python what piece of the string you want to grab.
🌐
Python Examples
pythonexamples.org › python-string-get-last-n-characters
Python String - Get last N characters
If you would like to use positive index while slicing the string, you may use the following expression to get the last N characters from the string in x. ... # Input string x = "Hello, World!" # Number of characters to retrieve n = 4 # Get the last n characters last_n_characters = x[len(x) - n:] # ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-remove-last-n-characters-from-a-string
Python program to remove last N characters from a string - GeeksforGeeks
July 23, 2025 - In this article, we’ll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions. String slicing is one of the simplest and most efficient ways to manipulate strings in Python.
🌐
Medium
medium.com › @ryan_forrester_ › remove-the-last-character-from-a-string-in-python-a16000d6c102
Remove the Last Character from a String in Python | by ryan | Medium
January 7, 2025 - Working with strings is a fundamental part of Python programming, and one common task is removing the last character from a string. Whether you’re cleaning up user input, processing text files, or manipulating data, knowing the different approaches to this task can make your code more efficient and readable. The fastest and most readable way to remove the last character from a string is using Python’s slice notation...
🌐
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
December 11, 2023 - In this example, we use a negative index to start at the end of the string and then specify that we want to stop at the last character. This returns the last character of the string, which is “o”. Here’s another way we can use slicing to extract the last letter of a string in Python.