Strings 

String is a sequence which is made up of one or more UNICODE characters. UNICODE was introduced to include every character in all languages and bring uniformity in encoding. Here the character can be a letter, digit, whitespace or any other symbol. A string can be created by enclosing one or more characters in single, double or triple quote. Let us take examples of creating strings using different type of quotes. 

>>> str1 = 'Hello World!' 

>>> str2 = "Hello World!" 

>>> str3 = """Hello World!""" 

>>> str4 = '''Hello World!''' 

str1, str2, str3, str4 are all string variables having the same value 'Hello World!'. Values stored in str3 and str4 can be extended to multiple lines using triple quotes as can be seen in the following 

example: 

>>> str3 = """Hello World! 

welcome to the world of Python""" 

>>> str4 = '''Hello World! 

welcome to the world of Python''' 

Accessing Characters in a String 

Each individual character in a string can be accessed using a technique called indexing. The index specifies the character to be accessed in the string and is written in square brackets. The index of the first character (from left) in the string is 0 and the last character is n-1 where n is the length of the string. If we give index value out of this range then we get an IndexError message. The index must be an integer (positive, zero or negative). 

#initializes a string str1 

>>> str1 = 'Hello World!' #gives the first character of str1 

>>> str1[0] 'H' #gives seventh character of str1 

>>> str1[6] 'W' #gives last character of str1 

>>> str1[11] '!' #gives error as index is out of range 

>>> str1[15] IndexError: string index out of range The index can also be an expression including variables and operators but the expression must evaluate to an integer. #an expression resulting in an integer index #so gives 6 character of str1 

>>> str1[2+4] 'W' #gives error as index must be an integer 

>>> str1[1.5] TypeError: string indices must be integers Python allows an index value to be negative also. Negative indices are used to access the characters of the string from right to left. Starting from right hand side, the first character has the index as -1 and the last character has the index –n where n is the length of the string. Table 13.1 shows the indexing of characters in the string „Hello World!‟ in both the cases, i.e., positive and negative indices. 

>>> str1[-1] #gives first character from right '!' 

>>> str1[-12] #gives last character from right 'H' 

Slicing 

In Python, to access some part of a string or sub-string, we use a method called slicing. This can be done by specifying an index range. Given a string st r1, the slice operation st r1[n:m] returns the part of the string str1 starting from index n (inclusive) and ending at m (exclusive). In other words, we can say that str1[n:m] returns all the characters starting from str1[n] till st r1[m-1]. The numbers of characters in the sub-string will always be equal to difference of two indices m and n, i.e., (m-n). 

>>> str1 = 'Hello World!' #gives substring starting from index 1 to 4 

>>> str1[1:5] 'ello' #gives substring starting from 7 to 9 

>>> str1[7:10] 'orl' #index that is too big is truncated down to #the end of the string 

>>> str1[3:20] 

lo World!' 

#first index > second index results in an #empty '' string 

>>> str1[7:2] If the first index is not mentioned, the slice starts from index. #gives substring from index 0 to 4 

>>> str1[:5] 'Hello' If the second index is not mentioned, the slicing is done till the length of the string. #gives substring from index 6 to end 

>>> str1[6:] 'World!' The slice operation can also take a third index that specifies the ‗step size‘. For example, str1[n:m:k], means every k character has to be extracted from the string str1 starting from n and ending at m-1. By default, the step size is one. 

>>> str1[0:10:2] 'HloWr' >>> str1[0:10:3] 'HlWl' Negative indexes can also be used for slicing. #characters at index -6,-5,-4,-3 and -2 are #sliced 

>>> str1[-6:-1] 'World' If we ignore both the indexes and give step size as -1 #str1 string is obtained in the reverse order 

>>> str1[::-1] '!dlroW olleH 

String Traversal Using for Loop: 

>>> str1 = 'Hello World!' 

>>> for ch in str1:

 print(ch, end = '') 

Hello World! 

#output of for loop In the above code, the loop starts from the first character of the string str1 and automatically ends when the last character is accessed.