Riverbed Technology Interview Question: Are strings mutable in python... | Glassdoor

Interview Question

Senior Quality Assurance Engineer Interview Sunnyvale, CA

Are strings mutable in python?

Tags:
python
Answer

Interview Answer

1 Answer

0

>>> a = 'str1'
>>> a[3] = '2'
Traceback (most recent call last):
  File "", line 1, in
TypeError: 'str' object does not support item assignment
>>>

But let's take a look at example:
>>> a = 'str1'
>>> b = 'str2'
>>> a = b
>>> print a
str2
>>>

List is mutable:
>>> a = ['s','t','r',1]
>>> print a
['s', 't', 'r', 1]
>>> a[3] = 2
>>> print a
['s', 't', 'r', 2]
>>>

strings are immutable in python. on Oct 15, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.