Interesting facts about strings in Python | Set 1
Last Updated :
06 Jul, 2022
1. Strings are Immutable
Once a string is defined, it cannot be changed.
a = 'Geeks'
print (a)
a[ 2 ] = 'E'
print (a)
|
Output:
Traceback (most recent call last):
File "/home/adda662df52071c1e472261a1249d4a1.py", line 9, in
a[2] = 'E'
TypeError: 'str' object does not support item assignment
But below code works fine.
a = 'Geeks'
print (a)
a = a + 'for'
print (a)
|
Output:
Geeks
Geeksfor
In the second program, interpreter makes a copy of the original string and then work on it and modifies it. So the expression a = a +’for’ doesn’t change string but reassigns the variable a to the new string generated by the result and drops down the previous string.
Understand using id() function.
id() function is used to return the identity of an object.
string1 = "Hello"
string2 = "Hello"
print ( id (string1))
print ( id (string2))
|
Output:
93226944
93226944
string1 and string2 both point to same object or same location.Now if someone try to modify any one of the string results will be different.
string1 = "Hello"
print ( id (string1))
string1 + = "World"
print (string1)
print ( id (string1))
|
Output:
93226944
'HelloWorld'
93326432
String1 is modified which contradict the fact that strings are immutable, but the identity before and after modification are different.It means a new object of the string1 is created after modification which confirms the fact that strings are immutable as no change was made in previous object of string1 rather a new one is created.
2. Three ways to create strings:
Strings in Python can be created using single quotes or double quotes or a triple quotes .
The single quotes and double quotes works same for the string creation.Talking about triple quotes, these are used when we have to write a string in multiple lines and printing as it is without using any escape sequence.
a = 'Geeks'
b = "for"
c =
d =
print (a)
print (b)
print (c)
print (d)
print (a + b + c)
|
Output:
Geeks
for
Geeks a portal
for geeks
He said, "I'm fine."
GeeksforGeeks a portal
for geeks
How to print single quote or double quote on screen?
We can do that in the following two ways:
- First one is to use escape character to display the additional quote.
- The second way is by using mix quote, i.e., when we want to print single quote then using double quotes as delimiters and vice-versa.
Example-
print ( "Hi Mr Geek." )
print ( "He said, \"Welcome to GeeksforGeeks\"" )
print ( 'Hey so happy to be here' )
print ( 'Getting Geeky, "Loving it"' )
|
Output:
Hi Mr Geek.
He said, "Welcome to GeeksforGeeks"
Hey so happy to be here
Getting Geeky, "Loving it"
How to print escape character instead?
If there is a requirement of printing the escape character(\) instead,then if user mention it in a string interpreter will think of it as escape character and will not print it.In order to print the escape character user have to use escape character before ‘\’ as shown in the example.
print ( " \\ is back slash " )
|
Output:
\ is back slash
Please Login to comment...