Python – Get Last N characters of a string
Last Updated :
11 Jul, 2023
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
Str = "Geeks For Geeks!"
N = 4
print ( Str )
while (N > 0 ):
print ( Str [ - N], end = '')
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
Str = "Geeks For Geeks!"
N = 4
print ( Str )
length = len ( Str )
Str2 = Str [length - N:]
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
Str = "Geeks For Geeks!"
N = 4
print ( Str )
Str2 = Str [ - N:]
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"
last_character = Str [ - 1 ]
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
Str = "Geeks For Geeks!"
N = 4
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
Str = "Geeks For Geeks!"
N = 4
print ( Str )
length = len ( Str )
import operator
Str2 = operator.getitem( Str , slice (length - N, length))
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
Str = "Geeks For Geeks!"
N = 4
print ( Str )
first, last_N, _ = Str .partition( Str [ - N:])
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
Share your thoughts in the comments
Please Login to comment...