Languages
[Edit]
EN

Python - get last 3 characters of string

0 points
Created by:
Maryamcc
511

In this article, we would like to show you how to get the last 3 characters of a string in Python.

Quick solution:

last_characters = string[-N:]

 

Overview

Get last N elements using [] operator:

string[start_index: end_index]

or

string[start_index: end_index: step]

Where:

  • start_index -  index position, from where it will start fetching the characters (default value is 0),
  • end_index -  index position, where it will end fetching the characters (default value is end of the string),
  • step - interval between each character (default value is 1)

Practical example

1. Get the last 3 characters of a string

In this example, we get the last 3 characters of the "dirask" string by index (-3 to end of the string).

string = "dirask"
last_characters = string[-3:]
print("The last 3 characters are:",last_characters)

 Output:

The last 3 characters are: ask
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - string (popular problems)

Python - get last 3 characters of string
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join