Question #6243   Submitted by Answiki on 06/08/2022 at 07:16:03 PM UTC

How to extract the last N characters of a string in Python ?

Answer   Submitted by Answiki on 06/08/2022 at 07:18:20 PM UTC

In Python, the syntax string[-N:] allows to extract the last N characters of a string:

substring = string[-N:]

Here are some examples:

string = "AnsWiki"

# Displays: ki
print ( string[-2:] )

# Displays: Wiki
print ( string[-4:] )

# Displays: AnsWiki
print ( string[-7:] )

# Displays: Wiki
print ( string[3:] )

Try these example online on repl.it.

2 events in history
Answer by Answiki on 06/08/2022 at 07:18:20 PM

In Python, the syntax string[-N:] allows to extract the last N characters of a string:

substring = string[-N:]

Here are some examples:

string = "AnsWiki"

# Displays: ki
print ( string[-2:] )

# Displays: Wiki
print ( string[-4:] )

# Displays: AnsWiki
print ( string[-7:] )

# Displays: Wiki
print ( string[3:] )

Try these example online on repl.it.

Question by Answiki 06/08/2022 at 07:16:03 PM
How to extract the last N characters of a string in Python ?
# ID Query URL Count

Icons proudly provided by Friconix.