Skip to content
Related Articles

Related Articles

Interesting facts about strings in Python | Set 1

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 06 Jul, 2022

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

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


Traceback (most recent call last):
  File "/home/", line 9, in 
    a[2] = 'E'
TypeError: 'str' object does not support item assignment

But below code works fine.

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



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.

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



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.

# Modifying a string
string1 = "Hello"
# identity of string1
string1 += "World"
# identity of modified string1



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.

# 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."'''
# Concatenation of strings created 
# using different quotes
print(a + b + c) 


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.


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"')                


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 Escape character
print (" \\ is back slash ")


 \ 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 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
Recommended Articles
Page :

Start Your Coding Journey Now!