Question

What is wrong with the following code? Explain verbally. s1 = "Canada" s1[2] = ‘T’ What...

What is wrong with the following code? Explain verbally.

s1 = "Canada"

s1[2] = ‘T’

What will the following code display? Explain verbally.

s1 = "Hawaii is a paradise"

print(s1[2:8])

Write puthon code using the in operator that determines whether ‘p’ is in s1.

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

Part -a) s1= "Canada"

             s1[2]= 'T'

             This will produce an error because strings in python are immutable i.e. you are not allowed to modify individual character of the existing string. You can just create a new string and make the changes. This is the thing which makes strings and other collections in python different from each other.

Part -b) s1 = "Hawaii is a paradise"

             print(s1[2:8])

            Output-

             waii i

             Basically, this notation s[m:n] return a sub-string of a string s starting from index m (s is a 0th indexing string) and ending at index n-1 i.e. index m is included and index n is not included.

Part -c) Write python code using the in operator that determines whether ‘p’ is in s1.

             'in' operator in python is a membership operator which returns True if a sequence with the specified value is present   in the object else it return False.

            Code -

             s1 = "Hawaii is a paradise"
             print ('p' in s1)

           Output -

            True

Add a comment
Know the answer?
Add Answer to:
What is wrong with the following code? Explain verbally. s1 = "Canada" s1[2] = ‘T’ What...
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
ADVERTISEMENT