Friday, December 28, 2018

What is immutable String ?

Hello World,
                                          The word immutable can confuse you in Python, What does this immutable meaning in Python ?, It looks strange for you, Don't worry Never loose your SMILE, in this post i am gonna make it clear, We all know about strings, what is strings ? and where to use these strings ?, We are unaware of the word immutable, what exactly does it mean ?. So Let's Start.

What is Immutable String in Python ? :

                                                       The String is immutable which means that once created than it can not be changed, The object created as a String is stored in the constant String Pool.

Python VS Java's Immutable String :

                                                                         Every immutable object in java is thread safe, that implies string is also thread safe, Strings can not be used by two threads simultaneously string once assigned can not be changed.
                                      
                                                As the name immutable stands for unchangeable, The immutable meaning is, it is unchangeable it will create the same copy of variable, But no-one can update the same variable in Python, As we all know that We need this update concept in programming, sometimes we need to update our variables in our program,we simply update our variable like int a = 1, than a = 7, this kind of thing.

We can update our variables in Python too, Now the term string, As we talk about in this post what is immutable string ?, As now you know what is immutable string ?.

In Python we can not update our string variable, we can update integer, float etc. each type of variable in Python But not String we can not update string variables in Python, Because Strings are immutable in Python.

So Let's See the Example of immutable string in Python :

# Immutable String in Python.
                     name = "Syntaxios"
                           print(name)
                                  name[0] = "B"          # Here it gives error. Because strings are immutable in Python
                          print(name)

But what if you want to change something in the given string, You can do that using replace() method. Let's See the example of replace() method.

# replace() method example.
           print(name)
                  print(name.replace("S", "B"))

At Last Thanks for Reading, Share with others if think it is useful thing to know.
, , , , ,

No comments:

Post a Comment