Python Tutorials – Strings

Python Tutorial on Strings. Handling quotes. Escape sequences. Immutability.

In this Python Tutorial, we will explore Strings, its properties and how to use it.

Python Strings can be defined by either enclosed with single quotes ” or double quotes “”.

Examples:

a = 'apple'
b = 'This is a single line String'
c = "foo"
d = "Double quotes also can be used"

Python also allows multi-line strings. This can be enclosed with triple quotes and can use either single quotes ” or double quotes “”.

Multi-line strings preserve the format when printed. The newline characters, spaces, tabs will be preserved.

Examples:

multi_string = """This is a multi-line String.
    This string can contain new line characters. 
    And the print statement also include new line characters."""

print(multi_string)

Output:

This is a multi-line String.
    This string can contain new line characters. 
    And the print statement also include new line characters.

What is the advantage of allowing both single quotes ” or double quotes “”?

If we want to enclose a word inside quotes, then we can do it in Python with proper combination of quotes.

Lets understand using the below example:

string = "This is a string in 'Python'"
print(string)

string = 'This is a string in "Python"'
print(string)

Output:

This is a string in 'Python'
This is a string in "Python"

What if the combination is wrong?

string = "This is a string in "Python""
print(string)

Output:

  File "<ipython-input-2-4bc957d26cee>", line 1
    string = "This is a string in "Python""
                                        ^
SyntaxError: invalid syntax

Iterable:

String is an array of characters in Python. So it is an iterable object. Which means we can loop through the characters of a string.

=> Also we can access the characters of string using the index

string = "Python"

for c in string:
    print(c)
    
print("\nWhat's in 4th index?")
print(string[4])

Output:

P
y
t
h
o
n

What's in 4th index?
o

Immutability:

=> Now you may think that using index, can we change the value of string?

=> No. String is an immutable object. That means you cannot change using index.

print(string[2])
string[2] = 'A'

Output:

t
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-d1c182eecdc5> in <module>
      1 print(string[2])
----> 2 string[2] = 'A'

TypeError: 'str' object does not support item assignment

If a value of a variable is changed, then it will not be changed in the existing memory. A new value will be created in new memory location. This is not only for Strings. It is also applicable for booleans, integers, floats in Python. We will also see other data structures which are all immutable later in this tutorial.

i.e., If the value is changed, the name will be same but the address location in memory will be different.

The function ID() can be used to know the Identity of a variable. This function returns a value which is a unique ID of the value stored in memory location. In some Python implementations, this can be a memory address. In some other Python implementations, this is just an another identifier which points to a value in memory location. However, we can use this function to check whether the variables are pointing to same object or different objects.

string = "Python"
print("ID of string = ", id(string))

string = "TestPython"
print("ID of string after change = ", id(string))

Output:

ID of string =  1772069741424
ID of string after change =  1772139624368

If the same string is copied to another variable, a new memory location will not be created. The same memory location will be pointed to both the strings.

string_1 = "Python"
print("ID of string_1 = ", id(string_1))

string_2 = string_1
print("ID of copied string, string_2 = ", id(string_2))

string_3 = "Python"
print("ID of string_3 = ", id(string_3))

Output:

ID of string_1 =  1772069741424
ID of copied string, string_2 =  1772069741424
ID of string_3 =  1772069741424

An Integer Example:

a = b = 10

print(id(a))
print(id(b))

b = b + 1

print(id(b))

Output:

140730960814784
140730960814784
140730960814816
Image by Author

Here, when b is assigned with b+a, the value is changed. So the value exists in the address location “140730960814784” will not be changed. Instead, a new memory location will be created and it will be named as b.


Escape Sequences:

Python handles escape sequences in 2 ways.

If the escape symbols are mentioned in normal string, then it will become a different string after compilation.

string = "C:\test\next\file.txt"
print(string)
print("===================")
string = "C:\west\ext\file.txt"
print(string)

Output:

C:	est
extile.txt
===================
C:\west\extile.txt

In the above result, the first string has escape characters “\t”, “\n” which changed the string after compilation.

But if the escape sequences are not matched then that will not be a problem.

In some cases, ‘unicodeescape’ codes cannot be decoded . This is because some codes are allotted for unicode only.

Please refer the below the official python documentation for string literals.

Image from Python official documentation
string = "Are you sure to delete the file? [Y\N]"
print(string)

In this case \N is malformatted. Either it should be mentioned as Y/N or Y\\N.

Output:

  File "<ipython-input-13-815a42b04fdc>", line 1
    string = "Are you sure to delete the file? [Y\N]"
            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 35-36: malformed \N character escape

Type #1: Use additional \

For example, we need to specify an file path string, we use \ symbol.

string = "C:\\test\\next\\file.txt"
print(string)

Output:

C:\test\next\file.txt

Type #2: Use raw string

Prefixed with r and enclosed with quotes will make the string as raw string, which means, whatever inside the quotes will not be processed like normal string and the escape sequences will be preserved.

raw_string = r'C:\the_folder\new_dir\file.txt'
print(raw_string)

Output:

C:\test\next\file.txt

Asha Ponraj
Asha Ponraj

Data science and Machine Learning enthusiast | Software Developer | Blog Writter

Articles: 86

Leave a Reply

Your email address will not be published. Required fields are marked *