1
website = 'http://www.python.org'
website[18:] = 'com'

The error says:

'str' object does not support item assignment.

Why is this code snippet not legal?

2 Answers 2

8

Because strings are immutable. Do it like this:

>>> website = 'http://www.python.org'
>>> website = website[:18] + 'com' # build a new string, reassign variable website
>>> website
'http://www.python.com'
Sign up to request clarification or add additional context in comments.

Comments

0

If you prefer not to count how many characters to stop before you reach .org

you can use

website=website[:len(website)-4]+".com"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.