Question

Write the output of the following code and explain the output. (10 marks) str='MIS 525' str[4]='-'...

Write the output of the following code and explain the output. (10 marks)

str='MIS 525'

str[4]='-'

print(str)

Error Message:

Traceback (most recent call last): File "number6.py", line 2, in <module> str[4]='-'

What is the reason why this code does not run? How can you fix it?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As strings are immutable in python we cannot change the elements in the string by indexing

WE can correct the error by joining lists by splicing

str='MIS 525'
# str[4]='-'
str=str[:3]+'-'+str[4:]
print(str)

The output is given below

Add a comment
Know the answer?
Add Answer to:
Write the output of the following code and explain the output. (10 marks) str='MIS 525' str[4]='-'...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT