Comment extraire les N derniers caractères d'une chaîne de caractères en Python ?
En Python, la syntaxe string[-N:] permet d'extraire les N derniers caractères d'une chaîne de caractères :
substring = string[-N:]
Voici quelques exemples :
string = "AnsWiki"
# Affiche : ki
print ( string[-2:] )
# Affiche : Wiki
print ( string[-4:] )
# Affiche : AnsWiki
print ( string[-7:] )
# Affiche : Wiki
print ( string[3:] )
Tester les exemples en ligne sur repl.it.
En Python, la syntaxe string[-N:] permet d'extraire les N derniers caractères d'une chaîne de caractères :
substring = string[-N:]
Voici quelques exemples :
string = "AnsWiki"
# Affiche : ki
print ( string[-2:] )
# Affiche : Wiki
print ( string[-4:] )
# Affiche : AnsWiki
print ( string[-7:] )
# Affiche : Wiki
print ( string[3:] )
Tester les exemples en ligne sur repl.it.
| # | ID | Query | URL | Count |
|---|