Python is the most sought-after programming language for many reasons. We can go on listing one by one and trust me, there are a lot of them, Let us consider the most important ones below:
- Python is a very powerful language for the reason that it supports a wide range of applications.
- The wide range includes Web Applications, Machine Learning, Artificial Intelligence, Image Processing, and Robotics.
- Python has a collection of a lot of powerful libraries and growing.
- The structure and syntax of the language are English-like and very easy to learn.
So with these points in mind, let us move ahead and look at the coding level to learn more about Strings in Python.
Before trying to understand better about strings, let us first understand what Mutable is.
What is Immutable in Python?
A straightforward understanding of immutable is the opposite of mutable. Mutable is when someone changes something. Immutable is something that cannot be changed, no matter what.
Is String in Python Immutable?
In Python strings and most other datatypes are immutable. The value of these datatypes cannot be changed once it is assigned. Hence they get the name immutable.
Why are Strings in Python called Immutable?
In any programming language, mutable is when the value of a string is changed from something that it is already initialized to something that the programmer wants it to be.
This is doable in most programming languages and the strings from those languages become mutable. But since this is not possible when it comes to Python, in Python it is immutable.
Let us understand this better by the following example.
web_site_title = "TechSorber" web_site_title = "k"
In the above example if you think when you print web_site_title after the execution of the two lines the output will be “TeckSorber”, you are wrong.
The above piece of the program outputs an error message. We are trying to change the value of the string web_site_title by replacing the fourth character “h” with k”. This is not doable in Python.
What if we have to Change the Value of a String?
A String can be changed using a new variable. We can do all the string operations to the new variable that we did for the first string and in the process, change it. For eg.
web_site_title = "TechSorber" web_site_new_title = "Teck"+ web_site_title[4:]
In the above program, we are assigning the changes done to the string web_site_title to a new string with the name web_site_new_title.
Is a string mutable?
No, strings are immutable. The assigned value cannot be changed like how we did in the above section.
Why is a String Immutable?
When a string is stored in a variable the programming language stores them in a storage device with references.
If we look at Python with respect to these references, they are all the same. This implies that the strings that are stored in the same references will not be changed.
To have a look at an example, let us consider the following program, an extension of the previous one.
web_site_title = "TechSorber" web_site_new_title = "Teck" + name_1[4:] print("id of web_site_title = ", id(web_site_title)) print("id of web_site_new_title = ", id(web_site_new_title))
The output of the above program is as follows:
id of web_site_title = 2614334952048 id of web_site_new_title = 2614335487664
You can see from the result that both the strings are stored in different references and have different IDs. On the contrary, the same string will have the same reference from the same location. The memory location will be different every time the variables are initialized. This can be understood better by the following program, again an extension of the previous one.
web_site_title = "Varun" print("id of web_site_title = ", id(web_site_title)) web_site_title = "Tarun" print("id of web_site_title afer initialing with new value = ", id(web_site_title))
The output of the above program is:
id of web_site_title = 2614335723888 id of web_site_title afer initialing with new value = 2614335486512
As you can see from the above result that the ids of the same variables are different before and after their initializations.