Kursinhalt
Data Types in Python
Data Types in Python
Slice the Phrase
Let's explore another valuable Python operation that proves beneficial when handling string data type. It can be advantageous to extract symbols at specified intervals. Ensure you review the example, as verbal explanations may not be the most effective method for grasping programming concepts (as exemplified by a famous quote from Albert Einstein in the example).
string = "Life is like riding a bicycle. To keep your balance, you must keep moving" sliced_string = string[1:11:4] print(sliced_string)
I want to elucidate the syntax string[starting_index : ending_index : step] to you. In this context, the resultant string has been generated from the first to the eleventh character, with a step size of 4, signifying that every fourth symbol within this range has been included.
Swipe to start coding
Now it's your turn! Follow these steps:
-
Use slicing to extract the phrase
"Get a foot"
from the first string and assign it to the variablephrase1
. -
Use slicing to extract the phrase
"away"
from the second string and assign it to the variablephrase2
(using negative indexing is recommended here).
Lösung
Danke für Ihr Feedback!
Slice the Phrase
Let's explore another valuable Python operation that proves beneficial when handling string data type. It can be advantageous to extract symbols at specified intervals. Ensure you review the example, as verbal explanations may not be the most effective method for grasping programming concepts (as exemplified by a famous quote from Albert Einstein in the example).
string = "Life is like riding a bicycle. To keep your balance, you must keep moving" sliced_string = string[1:11:4] print(sliced_string)
I want to elucidate the syntax string[starting_index : ending_index : step] to you. In this context, the resultant string has been generated from the first to the eleventh character, with a step size of 4, signifying that every fourth symbol within this range has been included.
Swipe to start coding
Now it's your turn! Follow these steps:
-
Use slicing to extract the phrase
"Get a foot"
from the first string and assign it to the variablephrase1
. -
Use slicing to extract the phrase
"away"
from the second string and assign it to the variablephrase2
(using negative indexing is recommended here).
Lösung
Danke für Ihr Feedback!