Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Défi : Récupérer des Lettres à Partir d'une Chaîne | Variables et Types en Python
Introduction à Python

book
Défi : Récupérer des Lettres à Partir d'une Chaîne

Tâche

Swipe to start coding

Vous avez la chaîne de caractères "This is string for learning" stockée dans la variable test_str.

Votre tâche :

  • Créez une variable first_char et assignez-lui la valeur du premier caractère de la chaîne test_str.
  • Créez une variable last_char et assignez-lui la valeur du dernier caractère de la chaîne test_str.
  • Créez une variable highlighted_word et extrayez le mot "learning" de la chaîne test_str.
  • Utilisez l'indexation pour résoudre cette tâche.

Solution

test_str = "This is string for learning"

first_char = test_str[0]

last_char = test_str[-1]

highlighted_word = test_str[-8 : 27]

# Testing
print(f"The first char of the string: {first_char};\nThe last char of the string: {last_char};\nThe highlighted word from the string: {highlighted_word}.")
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 10
test_str = "This is string for learning"

first_char = ___

last_char = ___

highlighted_word = ___

# Testing
print(f"The first char of the string: {first_char};\nThe last char of the string: {last_char};\nThe highlighted word from the string: {highlighted_word}")
toggle bottom row
some-alt