Computer Science

In Python, strings are immutable while lists are mutable. What is the difference?

Python Data Handling

18 Likes

Answer

In Python, strings are immutable means that individual letter assignment for strings is not allowed. For example:

name='hello'
name[0] = 'p'

The above Python code will cause an error as we are trying to assign some value to an individual letter of a string.

Lists are mutable in Python means that we can assign values to individual elements of a list. For example:

a = [1, 2, 3, 4, 5]
a[0] = 10

The above Python code will work correctly without any errors as Lists are mutable in Python.

Answered By

8 Likes


Related Questions