Given answer, B, is correct. But keep in mind too that even if the 'for' statement is corrected, the string is immutable, so assigning a new value to s[i] will fail with "'str' object does not support item assignment. HTH
strings can actually be modified by this type of loop. But the loop itself doesn't work, because it's just one number, 3. If it was iterable, it would have worked. Try running this:
s='abc'
for i in s:
s=s.upper()
print(s)
s is iterable, so the result is 'ABC'
Even if the code were
s="abc"
for i in range(len(s)):
>>>>s[i] = s[i].upper()
print(s)
it would still throw error as below:
Traceback (most recent call last):
File "<string>", line 5, in <module>
TypeError: 'str' object does not support item assignment
which was my initial thought
>>> s="abc"
>>> for s in len(s):
... s[i] = s[i].upper()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
upvoted 3 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
WorkingDaddy
Highly Voted 4 years, 4 months agoaed910c
Most Recent 10 months, 2 weeks agopincholinco
1 year agoN9
2 years, 2 months agomacxsz
2 years, 7 months agosmarty_arse
2 years, 10 months agorbishun
3 years, 1 month agochxzqw
3 years, 7 months agopremaseem
4 years, 3 months ago