What are Immutable Strings in Python?

Photo of author

Programs use variables that stand in for the storage locations in the computer’s memory to store data. The program’s state is defined as the information in the memory regions at any given time during execution.

mutable and immutable strings in Python

The article discusses about the two types of strings found in Python: Mutable and Immutable Strings.

See Also: What Are The Use Cases Of Python 

Contents

Mutable And Immutable Strings In Python

Python Strings come in two types: mutable and immutable strings. First, let’s discuss mutable strings.

Mutable Strings In Python

A mutable strings can have its state changed after it is created.

mutable strings in Python

A simple example of Mutable strings:

>> my_list = [‘lion’, ‘horse’, ‘tiger’]
>>> my_list
[‘lion’, ‘horse’, ‘tiger’]
>>> print(‘Address of my_list is: {}’.format(id(my_list)))
The address of my_list is: 139929780579206

The list has changed, but the memory address remains the same even if we wish to update the first value in the list and print it out. It altered the existing value. That’s what mutable means.

>> my_list[0] = ‘crocodile’
>>> my_list
[‘crocodile’, ‘horse’, ‘tiger’]
>>> print(‘Address of my_list is: {}’.format(id(my_list)))
The address of my_list is: 139929780579206

Let’s examine the memory locations of the list’s values now and see what happens before and after, when we alter the list’s first element’s value.

>> my_list
[‘crocodile’, ‘horse’, ‘tiger’]
>>> id(my_list)
139929780579206
>>> my_list[0]
‘crocodile’
>>> id(my_list[0])
139905997706993
>>> my_list[0] = ‘tiger’
>>> id(my_list[0])
139905997706897
>>> id(my_list)
139929780579206

It produces a new string object because a string cannot be changed. Memory addresses are not identical.

My list[0] has the id 139905997706993 when the first element’s value is “crocodile.” My list[0id] ‘s becomes 139905997706897 when the value is changed to “tiger.” You’ll see that the IDs differ.

A list retains the same address even if we modify its value or change its order. The value you changed will have a different address at that location. My list still had the same id, which was 139929780579206.

Immutable Strings In Python

And now, let’s talk about immutable strings in Python:

Immutable strings are a core concept in the Python programming language, and they are strings that you cannot change once you create them. Immutability makes strings more efficient to work with and safer from security risks.

The ‘str‘ data type represents immutable strings in Python. You can create strings using single or double quotes. Strings can contain any characters, including whitespace, numbers, and symbols. Strings can also span multiple lines.

Immutable strings PythonA string cannot be altered or changed in any manner once it has been produced. If you need to change a string, you must make the necessary modifications to a new string. This means that strings are immutable objects in Python, which makes them more efficient to work with than mutable objects.

Immutability also makes strings safer from security risks. Since you cannot modify strings, they are less vulnerable to malicious attacks. When working with strings containing sensitive information, such as passwords or private information, this is highly crucial.

Python provides several functions and methods for working with strings. These include functions for finding and replacing substrings, concatenating strings, and trimming whitespace. Additionally, Python offers functions for translating strings into different data types like floating and integers.

Python also offers several string formatting techniques. These techniques allow for the creation of strings with custom formatting, including strings with dates, currency, and other formats.

In addition to the core string functions, Python has many libraries that provide additional functionality for working with strings. These libraries include regex (regular expression) functions for more advanced string manipulation and libraries for working with text files.

Immutability has the benefit of guaranteeing that a string’s value stays constant throughout a program because no other code can change it. Debugging becomes more straightforward and reduces the errors. The str() method constructs a string from a supplied value and creates immutable strings. For instance, the following code turns the number 6 into a string: my_string = str(6). You cannot alter the string’s value once you create it. It will generate an error if you attempt to change the string. For instance, the following code will produce an error:

my_string = “6” my_string[0] = “7”
In this illustration, we make an effort to alter the string’s first character.
Let’s look at another example of immutable strings.
>>> phrase = ‘how are you today’
>>> phrase
how are you today
>>> phrase = ‘what are you doing’
>>> phrase
what are you doing

It’s making a new string object rather than altering the existing one. We can use the id function we previously learned to see this in greater detail. Remember to print the memory address, use the id() method.

>> phrase = ‘how are you today’
>>> print(‘Address of phrase is: {}’.format(id(phrase)))
The address of the phrase is: 139929798564765
>>> phrase = ‘what are you doing’
>>> print(‘Address of phrase is: {}’.format(id(phrase)))
The address of the phrase is: 139929793265789

It produces a new string object because a string cannot be changed. Memory addresses are not identical.

Conclusion

Overall, immutable strings are an essential concept in Python. They are strings that you cannot change once you create them. Immutability makes strings more efficient to work with and safer from security risks.

See Also: How To Fix SyntaxError Unexpected EOF While Parsing?

 

Leave a Comment