The Wayback Machine - https://web.archive.org/web/20250105020245/https://www.geeksforgeeks.org/python-get-last-n-characters-of-a-string/
Open In App

Python – Get Last N characters of a string

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a string and an integer N, the task is to write a Python program to print the last N characters of the string. 

Example

Here we are using the concept of positive slicing where we are subtracting the length with n i.e. to get the positive index for the desired substring.

Python
# get input
Str = "Geeks For Geeks!"
N = 4; length = len(Str)

# create a new string of last N characters
Str2 = Str[length - N:]
print(Str2)

Output
eks!

Get the last N characters of a String using Loop

Using a loop to get to the last n characters of the given string by iterating over the last n characters and printing them one by one.

Python
# get input
Str = "Geeks For Geeks!"
N = 4

# print the string
print(Str)

# iterate loop
while(N > 0):

  # print character
    print(Str[-N], end='')

    # decrement the value of N
    N = N-1

Output
Geeks For Geeks!
eks!

String Slicing with Negative Indexing

Here we are using the concept of negative slicing where we are not subtracting the length with n.

Python
# get input
Str = "Geeks For Geeks!"
N = 4

# print the string
print(Str)

# create a new string of last N characters
Str2 = Str[-N:]

# print Last N characters
print(Str2)

Output
Geeks For Geeks!
eks!

Get the last characters of a string in Python

In Python, negative indexing allows you to access elements from the end of a sequence. So Str[-1] will give you the last character of the string.

Python
Str = "GeeksforGeeks"

#using negative index to pick the last element of the string
# Here -1 refers to the last character index in the string

last_character = Str[-1]

#printing the last character 
print(last_character)

Output
s

Get the Last N Characters of a String Using Recursion

Here we use a recursive function that progressively reduces the length of the string in recursion to get desired last N character that we won’t get

Python
# get input
Str = "Geeks For Geeks!"
N = 4
 
# print the string
print(Str)

def printLastNChar(Str, N): 
    l = len(Str) 
    if N > 0: 
        print(Str[l - N], end = "") 
        printLastNChar(Str, N - 1) 

printLastNChar(Str, N) 

Output
Geeks For Geeks!
eks!

Get Last N Characters of a String Using Operator.getitem()

Used operator.getitem(), slice() to extract the sliced string from length-N to length and assigned it to Str2 variable then displayed the Str2 variable.

Python
# get input
Str = "Geeks For Geeks!"
N = 4

# print the string
print(Str)

# get length of string
length = len(Str)
import operator
# create a new string of last N characters
Str2 = operator.getitem(Str, slice(length-N, length))

# print Last N characters
print(Str2)

Output
Geeks For Geeks!
eks!

Get the Last N Characters of a String Using the Partition and str[-N:] Methods

Split the string into 3 parts using the partition method and then print the result

Python
# get input
Str = "Geeks For Geeks!"
N = 4

# print the string
print(Str)

# split the string into 3 parts
first, last_N, _ = Str.partition(Str[-N:])

# print the result
print(last_N)

Output
Geeks For Geeks!
eks!


Next Article

Similar Reads

three90RightbarBannerImg