Partition de chaîne en Python

Niveau 3114 frappes

Ceci divise une chaîne en trois parties avec partition(). Elle retourne la partie avant, le séparateur et la partie après.

Code Python

# Une adresse e-mail à couper au @
1text = "hello@world.com"
# partition("@") coupe au premier @ et renvoie (avant, séparateur, après)
# Après déballage : local="hello", separator="@", domain="world.com"
2local, separator, domain = text.partition("@")
# Seuls local et domain servent : affiche "Local: hello, Domain: world.com"
3print(f"Local: {local}, Domain: {domain}")

Sortie

Local: hello, Domain: world.com
S'entraîner à taper ce code

Entraînez-vous à taper ce code. Les résultats ne sont pas enregistrés dans le classement.

Symboles de ce code

Les symboles que vous tapez dans ce code Python et leur nombre d'occurrences.

  • "× 6
  • ,× 3
  • :× 2
  • .× 2
  • (× 2
  • )× 2
  • {× 2
  • }× 2
  • @× 2
  • =× 2

Autres exemples Python de niveau 3

← Entraînement à la frappe Python