As string is immutable,so we can't change the string so how we can insert a character at middle position?
code:
s = "hello world"
s[5] = '-'
But it gives you error as it is immutable.so,how we can resolve this problem?
As string is immutable,so we can't change the string so how we can insert a character at middle position?
code:
s = "hello world"
s[5] = '-'
But it gives you error as it is immutable.so,how we can resolve this problem?
We know string is immutable,but we can't change values through assignment operator.so we can acheive this through string slicing:
s = s[:5]+'-'+s[6:]
so now s becomes "hello-world". so this can be done using string slicing.
s is now a new string. If s was passed as an argument to a function, the value of s in the function is now hello-world, but the caller still sees the original string hello worlds and remembers to declare it global.