Comment extraire N caract�res d'une cha�ne en Python ?
En Python, la syntaxe ?string[i:j]
permet d'extraire les caract�res entre les indices ?i
(inclus) et ??j
(exclus). Notons que le premier caract�re de la cha�ne se trouve � l'indice 0.
substring = string[i:j]
Voici quelques exemples :
string = "AnsWiki"
# Affiche : Wi
print ( string[3:5] )
# Affiche : Wiki
print ( string[3:7] )
# Affiche : AnsWiki
print ( string[0:7] )
# Affiche : Ans
print ( string[0:-4] )
# Affiche : Wi
print ( string[-4:-2] )
Tester l'exemple en ligne sur repl.it.
En Python, la syntaxe ?string[i:j]
permet d'extraire les caract�res entre les indices ?i
(inclus) et ??j
(exclus). Notons que le premier caract�re de la cha�ne se trouve � l'indice 0.
substring = string[i:j]
Voici quelques exemples :
string = "AnsWiki"
# Affiche : Wi
print ( string[3:5] )
# Affiche : Wiki
print ( string[3:7] )
# Affiche : AnsWiki
print ( string[0:7] )
# Affiche : Ans
print ( string[0:-4] )
# Affiche : Wi
print ( string[-4:-2] )
Tester l'exemple en ligne sur repl.it.
# | ID | Query | URL | Count |
---|---|---|---|---|
0 | 814 | extraire lettres dans chaine de caract�res ^python | https://fr.ans.wiki/2794/comment-extraire-n-caracteres-d-une-chaine-en-python | 1 |