Chapter 8 Strings
Chapter 8 Strings
» String Operations
» Traversing a String
» Strings Method and Built-in Functions
» Handling Strings
1.STRINGS
str1, str2, str3, str4 are all string variables having the same value 'Hello World!'.
An inbuilt function len() in Python returns the length of the string that is passed as parameter. For
example, the length of string str1 = 'Hello World!' is 12.
#gives the length of the string str1
>>> len(str1)
12
#length of the string is assigned to n
>>> n = len(str1)
>>> print(n)
12
#gives the last character of the string
>>> str1[n-1]
'!'
#gives the first character of the string
>>> str1[-n]
'H'
String is Immutable
A string is an immutable data type. It means that the contents of the string cannot be changed
after it has been created.
>>> str1 = "Hello World!" #if we try to replace character 'e' with 'a'
>>> str1[1] = 'a'
TypeError: 'str' object does not support item assignment
3. STRING OPERATIONS
Concatenation
To concatenate means to join.
Python allows us to join two strings using concatenation operator plus which is denoted by
symbol +
>>> str1 = 'Hello' #First string
>>> str2 = 'World!' #Second string
>>> str1 + str2 #Concatenated strings
'HelloWorld!'
#str1 and str2 remain same
#after this operation.
>>> str1
'Hello'
>>> str2
'World!'
Repetition
Python allows us to repeat the given string using repetition operator which is denoted by
symbol *.
Membership:
Python has two membership operators 'in' and 'not in'.
The 'in' operator takes two strings and returns True if the first string appears as a substring
in the second string, otherwise it returns False.
>>> str1 = 'Hello World!'
>>> 'W' in str1
True
>>> 'Wor' in str1
True
>>> 'My' in str1
False
The 'not in' operator also takes two strings and returns True if the first string does not
appear as a substring in the second string, otherwise returns False.
>>> str1 = 'Hello World!'
>>> 'W' in str1
True
>>> 'Wor' in str1
True
>>> 'My' in str1
False
Slicing
In Python, to access some part of a string or substring, we use a method called slicing.
This can be done by specifying an index range.
We can say that str1[n:m] returns all the characters starting from str1[n] till str1[m-1].
>>> 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]
4. TRAVERSING A STRING:
We can access each character of a string or traverse a string using for loop and while loop.
(A) String Traversal Using for Loop:
>>> str1 = 'Hello World!'
>>> for ch in str1:
print(ch,end = '')
Hello World! #output of for loop
The loop starts from the first character of the string str1 and automatically ends when the last
character is accessed.
(B) String Traversal Using while Loop:
>>> str1 = 'Hello World!'
>>> index = 0
#len(): a function to get length of string
>>> while index < len(str1):
print(str1[index],end = '')
index += 1
Hello World! #output of while loop
Here while loop runs till the condition index< len(str) is True, where index varies from 0 to
len(str1) -1.