Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

WGU Foundations of Programming Python Exam - Topic 2 Question 6 Discussion

Actual exam question for WGU's Foundations of Programming Python exam
Question #: 6
Topic #: 2
[All Foundations of Programming Python Questions]

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]

Show Suggested Answer Hide Answer
Suggested Answer: A

==========

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


Contribute your Thoughts:

0/2000 characters
France
17 days ago
I feel like I’ve seen this before; it’s probably just a simple fix to change the 2 to a 3.
upvoted 0 times
...
Rolande
22 days ago
I remember practicing a similar question where we had to fix a range in a list. I think it’s definitely off by one.
upvoted 0 times
...
Ricki
27 days ago
I think the slicing should go up to index 3 instead of 2, but I'm not entirely sure.
upvoted 0 times
...

Save Cancel