KnowledgeBoat Logo

Computer Science

Can you say strings are character lists? Why? Why not?

Python String Manipulation

19 Likes

Answer

Strings are sequence of characters where each character has a unique index. This implies that strings are iterable like lists but unlike lists they are immutable so they cannot be modified at runtime. Therefore, strings can't be considered as character lists. For example,

str = 'cat'
# The below statement
# is INVALID as strings
# are immutable
str[0] = 'b' 

# Considering character lists
strList = ['c', 'a', 't']
# The below statement
# is VALID as lists
# are mutable
strList[0] = 'b'

Answered By

11 Likes


Related Questions