SIMULATION
Fix the off-by-one error in this function that should return the first 3 characters of a string.
def first_three(text):
return text[0:2]
==========
Step 1: Python string slicing uses this format:
text[start:stop]
Step 2: The start index is included.
Step 3: The stop index is excluded.
Step 4: To return the first 3 characters, start at index 0 and stop at index 3.
Correct code:
def first_three(text):
return text[0:3]
Simplified correct code:
def first_three(text):
return text[:3]
Example:
print(first_three('Python'))
Output:
Pyt
France
17 days agoRolande
22 days agoRicki
27 days ago