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

SIMULATIONFix 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]
A) See the Step by Step Solution below in Explanation

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
Herschel
9 hours ago
I’m a bit confused about whether it should be `text[0:3]` or `text[:3]`. Both seem like they could work.
upvoted 0 times
...
Tamera
6 days ago
I remember practicing a similar question where we had to fix a range in a loop. It feels like it's the same concept here.
upvoted 0 times
...
Louisa
11 days ago
I think the slicing should go up to index 3 instead of 2, but I'm not completely sure.
upvoted 0 times
...
Tamar
16 days ago
I’m a bit confused about how slicing works. Does it include the last index or not?
upvoted 0 times
...
France
2 months 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
2 months 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
2 months 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