NAVODAYA VIDYALAYA SAMITI�HYDERABAD REGION
E-CONTENT
COMPUTER SCIENCE CLASS XI
By: Meenakshi Sharma
PGT Computer Science JNV Hassan
COMPUTATIONAL THINKING AND PROGRAMMING-1
Strings
Learning Objectives
Strings
String is a sequence which is made up of one or more UNICODE characters. 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.
Example:
>>> 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!'.
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. The index must be an integer (positive, zero or negative).
>>> str1 = 'Hello World!‘
>>> str1[0]
'H‘
>>> str1[6]
'W‘
>>> str1[11]
'!'
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. An attempt to do this would lead to an error.
>>> str1 = "Hello World!"
#if we try to replace character 'e' with 'a'
>>> str1[1] = 'a'
TypeError: 'str' object does not support item assignment
Strings Operators
There are several operators that can be used to manipulate the strings in several ways.
There are Basic Operators(+ and *), Membership Operators (In and Not In) and Comparison Operators(all relational operators)
Basic Operators:
Strings Operators
Basic Operators:
Strings Operators
Membership Operators:
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.
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.
Strings Slicing
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. Given a string str1, the slice operation str1[n:m] returns the part of the string str1 starting from index n (inclusive) and ending at m (exclusive) i.e. str1[n:m] returns all the characters starting from str1[n] till str1[m-1].
The numbers of characters in the substring will always be equal to difference of two indices m and n, i.e., (m-n).
Strings Slicing
Slicing :
If the first index is not mentioned, the slice starts from index 0.
>>> str1[:5] #gives substring from index 0 to 4
'Hello'
If the second index is not mentioned, the slicing is done till the length of the string.
>>> str1[6:] #gives substring from index 6 to end
'World!'
The slice operation can also take a third index that specifies the ‘step size’. For example, str1[n:m:k], means every kth 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] will give output 'HloWr'
>>> str1[0:10:3] will give output 'HlWl'
Strings Traversal
String Traversal:
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
In the above code, the loop starts from the first character of the string str1 and automatically ends
when the last character is accessed.
Strings Traversal
String Traversal:
We can access each character of a string or traverse a string using for loop and while loop.
(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.
Strings Built-in Functions
String Methods and Built-in Functions:
Python has several built-in functions that allow us to work with strings.
Strings Built-in Functions
String Methods and Built-in Functions:
Strings Built-in Functions
String Methods and Built-in Functions:
Strings Built-in Functions
String Methods and Built-in Functions:
Strings Built-in Functions
String Methods and Built-in Functions:
Strings Built-in Functions
String Methods and Built-in Functions:
Strings Built-in Functions
String Methods and Built-in Functions:
Strings Built-in Functions
String Methods and Built-in Functions:
Strings
Strings
Summary
BIBLIOGRAPHY:
1. Computer Science Textbook for class XI
by NCERT
2. Computer Science with Python
by Sumitha Arora
3. Computer Science with Python
by Preeti Arora