In the previous two chapters we have studied about arrays and other in-built data structures like lists, tuples, sets and dictionaries. In this chapter we will discuss another in-built data type/data structure of Python, and that is string. In programming languages, string handling is a very important task and in data structure we will learn about different algorithms for efficient string handling.
5.1 Introduction
A string is a sequence of characters allocated contiguously in memory where the characters may be letters of the alphabet, digits, punctuation marks, white spaces or any other symbols. In Python a string is implemented as an object of the in-built class str. To define a string constant we may use single quotation (‘ ’), double quotation (“ ”) or triple quotation (‘“ ”’). So, ‘India’, “India” and ‘“India”’ – all three are valid string constants in Python. Apart from a normal string, there are two other types of strings. These are escape sequences and raw strings. Like C, C++ and Java programming, Python also supports escape sequences. An escape sequence is the combination of a backslash (\) and another character that together represent a specific meaning escaping from their original meaning when used individually. Table 5.1 shows some escape sequences in Python.
The following example shows the use of escape sequences:
Example 5.1: Example to show the use of escape sequences.
print(“Hello \nClass!!”)
Output:
Hello
Class!!
print(“Hello \tClass!!”)
Output:
Hello Class!!
print(‘Cat\rM’)
Output:
Mat
print(‘Cat\b\bu’)
Output:
Cut
print(“Welcome to the world of \‘Python\’”)
Output:
Welcome to the world of ‘Python’
print(“\”India\” is great.”)
Output:
“India” is great.
If a string is preceded by an ‘r’ or ‘R’, then it is called a raw string. Within a raw string, escape sequence do not work. They behave like a normal character. Consider the following example:
Example 5.2: Example to show the use of raw string.
print(‘Techno\nIndia’)
Output:
Techno
India
print(r‘Techno\nIndia’)
Output:
Techno\nIndia
print(‘Techno\\India’)
Output:
Techno\India
print(R‘Techno\\India’)
Output:
Techno\\India
5.2 Basic String Operations
Strings are immutable in Python. Hence, we cannot modify a string. But we can create a new string after manipulating an old string. We can extract an individual character or portion of a string. Characters within a string are accessed using the subscript operator and the index value.