Interesting facts about strings in Python | Set 1

1. Strings are Immutable
Once a string is defined, it cannot be changed.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to show that 
# string cannot be changed
  
a = 'Geeks'
  
# output is displayed
print(a)
  
a[2] = 'E'
print(a) # causes error

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to show that 
# a string can be appended to a string.
  
a = 'Geeks'
  
# output is displayed
print(a)
a = a + 'for'
  
print(a) # works fine

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to show that
# both string hold same identity
  
string1 = "Hello"
string2 = "Hello"
  
print(id(string1))
print(id(string2))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Modifying a string
  
string1 = "Hello"
  
# identity of string1
print(id(string1))
  
string1 += "World"
print(string1)
  
# identity of modified string1
print(id(string1))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to create  
# strings in three different
# ways and concatenate them.
  
# string with single quotes
a = 'Geeks'
  
# string with double quotes
b = "for"
  
# string with triple quotes
c = '''Geeks a portal 
for geeks'''
  
d = '''He said, "I'm fine."'''
  
print(a)
print(b)
print(c)
print(d)
  
  
# Concatenation of strings created 
# using different quotes
print(a + b + c) 

chevron_right


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-

filter_none

edit
close

play_arrow

link
brightness_4
code

print("Hi Mr Geek.")
  
# use of escape sequence
print("He said, \"Welcome to GeeksforGeeks\"")    
  
print('Hey so happy to be here')
  
# use of mix quotes
print ('Getting Geeky, "Loving it"')                

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Print Escape character
print (" \\ is back slash ")

chevron_right


Output:

 \ is back slash

This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : tuhinpaul