Open In App

Interesting facts about strings in Python | Set 1

Last Updated : 06 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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


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


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


Output:

 \ is back slash


Similar Reads

Py-Facts - 10 interesting facts about Python
Python is one of the most popular programming languages nowadays on account of its code readability and simplicity. All thanks to Guido Van Rossum, its creator. I've compiled a list of 10 interesting Facts in the Python Language. Here they are:1. There is actually a poem written by Tim Peters named as THE ZEN OF PYTHON which can be read by just wri
4 min read
Interesting facts about strings in Python | Set 2 (Slicing)
Creating a String Strings in Python can be created using single quotes or double quotes or even triple quotes. # Python Program for # Creation of String # Creating a String # with single Quotes String1 = 'Welcome to the Geeks World' print("String with the use of Single Quotes: ") print(String1) # Creating a String # with double Quotes String1 = "I'
4 min read
Interesting facts about data-types and modifiers in C/C++
Here are some logical and interesting facts about data-types and the modifiers associated with data-types:- 1. If no data type is given to a variable, the compiler automatically converts it to int data type. C/C++ Code #include <iostream> using namespace std; int main() { signed a; signed b; // size of a and b is equal to the size of int cout
4 min read
Interesting Facts about C++
C++ is a general-purpose, object-oriented programming language. It supports generic programming and low-level memory manipulation. Bjarne Stroustrup (Bell Labs) in 1979, introduced the C-With-Classes, and in 1983 with the C++. Here are some awesome facts about C++ that may interest you: The name of C++ signifies the evolutionary nature of the chang
2 min read
Interesting Facts about Linux
Linux is community-developed and open source operating system for computers, servers, mainframes, embedded devices, and mobile devices. It is supported on nearly every major computer platforms including SPARC, ARM and, x86 building it as one of the most extensively supported operating systems. Here are some facts about Linux that may interest you:
2 min read
Interesting Facts about C#
C# is a general-purpose, modern and object-oriented programming language pronounced as "C Sharp" or "C Hash". It was developed by Microsoft led by Anders Hejlsberg and his team within the .NET initiative and was approved by the European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO). C# is among the languag
3 min read
Interesting Facts About Java
Java: A general-purpose, high-level programming language. It is developed by Sun Microsystems. It was developed by a mini team of engineers which is known as the Green Team. They initiated this language in 1991. Here are some interesting facts about Java: The initial name of java was "Oak". It was changed to Java by Sun's marketing department chang
2 min read
Interesting Facts about PYGAME
Pygame is a set of python module which is used in designing video games. In Pygame, there are computer graphics and sound libraries in order to develop high quality and user interactive games. Pygame was developed by Pete Shinners. Till 2000, it was a community project, later on it was released under open source free software General Public License
2 min read
Interesting Facts about Macros and Preprocessors in C
In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. by this we mean to say that the ‘#’ symbol is used to process the functionality prior than other statements in the program, that is, which means it processes some code before run-time or say during the compile-time. In a ver
6 min read
Interesting facts about Increment and Decrement operators in Java
Increment operator is used to increment a value by 1. There are two varieties of increment operator: Post-Increment: Value is first used for computing the result and then incremented.Pre-Increment: Value is incremented first and then the result is computed. Example Java Code //INCREMENTAL OPERATOR //Let's see what exactly happens when we use these
5 min read
Practice Tags :
three90RightbarBannerImg