Python: How to get Last N characters in a string?

This article is about finding the last N characters of String where the N represents any number or int value.

How to get the last N characters in a String using Python

In Python, String is a sequence of characters where character can be a string, integer or any symbol. With each character the index numbers are associated with each character.

For example : We have a String variable named String_value which contains a String Technology i.e

String_value = “Digital”

The first character starts with index 0 and it goes on. Like

Index of character ‘D’ = 0

Index of character ‘i’ = 1

Index of character ‘g’ = 2

Index of character ‘i’ = 3

Index of character ‘t’ = 4

Index of character ‘a’ = 5

Index of character ‘l’  = 6

In Python, just by passing the index number in [ ] (Subscript operator or Index Operator) any character of the string can be accessed.

Like String_value[i] will return i-th character of the string, where ‘i’ represents an index position.

For example, String_value[4] will return character ‘t’.

Get last character of the String using negative indexing :

Negative indexing refers to associate negative index value to each character of the string. Like last character of string will be associated with index number -1 then negative index will go on incrementing and characters will go on decrementing so that each character will be associated with a negative index.

Let’s see how actually its implemented

String_value = “Digital”

Where the sequence number/index of the last character starts with -1 and it goes on. Like

Index of character ‘D’ = -7

Index of character ‘i’ = -6

Index of character ‘g’ = -5

Index of character ‘i’ = -4

Index of character ‘t’ = -3

Index of character ‘a’ = -2

Index of character ‘l’  = -1

So let’s see a program to get the last character of the string using negative indexing.

#Program:

String_value = "Digital"
Last_char = String_value[-1]
print('Last character of the string is', Last_char)
Output : 
Last character of the string is l

Here, just by passing -1 in the operator [ ], we fetched the last character of the string.

Get the last character of string using len() function :

Without using negative indexing we can also get the last character of the string just by finding the length of the string and then printing the character at length-1 position.

So let’s see a program to get the last character of the string using len( ) function.

#Program: 

String_value = "Digital" 
#Finding length of String_value
length = len(String_value )
# Finding the last character of string
#Length of the string -1 represents last character
Last_char = String_value[length -1]
print('Last character of the string is', Last_char)
Output :
Last character of the string is l

Get last N characters in a string :

In above examples we only have passed index position in the subscript operator, i.e. [] but it can also accept a range too i.e.

Syntax : string_value[ start_index_position : end_index_position : step_size]

Where,

  • start_index_position: It refers to the index position, from where it will start fetching the characters in a string (Default value is 0).
  • end_index_position: It refers to the index position, upto which it will fetch the characters from string (Default value is end of string).
  • step_size: It refers to the interval between each character (Default value is 1).

When we use the above statement it returns a substring means part of the string.

So let’s see a program to understand the implementation of it.

Approach #1 : Using negative index

#Program: 

String_value = "Digital" 
#Finding length of String_value
length = len(String_value )
# Finding the last 3 characters of string
# Here mention start index i.e -3 
# End index not mentioned, so it will go upto last character
Last_char = String_value[-3:]
print('Last 3 characters of the string is', Last_char)
Output :
Last 3 characters of the string is tal

Approach #2 : Using len( ) function

#Program: 

String_value = "Digital" 
#Finding length of String_value 
length = len(String_value ) 
# Finding the last 3 character of string 
#here start index is length of the string -3 i.e length-3
Last_char = String_value [length - 3 :]
print('Last 3 characters of the string is', Last_char)
Output :
Last 3 characters of the string is tal