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 ?
Merged questions
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.
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 |
|---|---|---|---|---|
| 0 | 12930 | en | https://en.ans.wiki/6243/how-to-extract-the-last-n-characters-of-a-string-in-python | 6 |
Icons proudly provided by Friconix.