Open In App

Python – Get Last N characters of a string

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Input: Geeks For Geeks!; N = 4
Output: eks!
Explanation: The given string is Geeks For Geeks! and the last 4 characters is eks!.

Input: PYTHON; N=1
Output: N
Explanation: The given string is PYTHON and the last character is N.

Get the last N Characters of a String

Below are the lists of methods that we will cover in this article:

  • Using a Python loop
  • Using List slicing
  • Using string slicing with negative indexing
  • Using Recursion in Python

Using a loop to get the last N characters of a string

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.

Python3




# 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!

Get the last N characters of a string using Slicing

Python list slicing is used to print the last n characters of the given string.

Using string slicing with positive indexing 

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
 
# print the string
print(Str)
 
# get length of string
length = len(Str)
 
# create a new string of last N characters
Str2 = Str[length - N:]
 
# print Last N characters
print(Str2)


Output:

Geeks For Geeks!
eks!

Using string slicing with negative indexing

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

Python3




# 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.

Python3




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

# Time Complexity: O(1)
# Auxiliary Space: O(1)

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

Python3




# 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!

# Time Complexity: O(n)
# Auxiliary Space: O(n)

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.

Python3




# 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!

# Time Complexity: O(n)
# Auxiliary Space: O(n)

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

Python3




# 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!

Time complexity: O(N), where N is the length of the substring to be reversed.
Auxiliary space: O(N), since we create a list of N characters to store the reversed substring.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads