Thursday, December 26, 2024
Google search engine
HomeLanguagesInteresting facts about strings in Python | Set 1

Interesting facts about strings in Python | Set 1

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
print(a)
  
a[2] = 'E'
print(a) # causes error


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.




# 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


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.




# Python3 program to show that
# both string hold same identity
  
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.




# Modifying a string
  
string1 = "Hello"
  
# identity of string1
print(id(string1))
  
string1 += "World"
print(string1)
  
# identity of modified 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.




# 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 Lazyroar'''
  
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) 


Output:

Geeks
for
Geeks a portal 
for Lazyroar
He said, "I'm fine."
Lazyroar a portal 
for Lazyroar

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.")
  
# use of escape sequence
print("He said, \"Welcome to Lazyroar\"")    
  
print('Hey so happy to be here')
  
# use of mix quotes
print ('Getting Geeky, "Loving it"')                


Output:

Hi Mr Geek.
He said, "Welcome to Lazyroar"
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 ")


Output:

 \ is back slash

This article is contributed by Arpit Agarwal. If you like Lazyroar and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar 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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments