Class XI
Computer Science
GIST-7: Strings
STRING HANDLING
1. Some General Properties of Strings:
• Python stores a sequence of characters as a string within a pair of single or double quotes like ‘abc’ or “abc”
• String is an immutable data type. You cannot change a string in place. When a string is changed, a new string is
created
• An index system is used to access each character in the string. The index of the first
character is zero (0) and the index of the last character is 1 less than the string length
• You put the index in a pair of square brackets [ ] to access a character
• Strings can be single line or multiline
2. Traversing a String:
Traversing means accessing every character of a string from one end to the other.
Method-1: Without using string Method-2: Using string index.
index. river = ‘Ganges’
river = ‘Ganges’ for i in range( len(river) ):
for c in river: print( river[ i ] )
print( c )
Working with Negative Index
A negative index system can also be used to work with a string
• The last character is given a negative index of -1
• The index decreases from right to left up to the first character, which has an index of –(string length)
• When Python sees a negative index, it adds the length of the string to the negative index to get the positive index
river = ‘Ganges’
for i in range( -1, -len(river)-1, -1 ):
print( river[ i ] )
3. Basic String Operations:
Two basic operations possible on a string.
String Concatenation: The + Operator is used to join two strings side by side. It creates a New String by joining the two strings
Examples: s1 = ‘Da’ + ‘Vinci’ s1 = ‘DaVinci’
s2 = ‘221’ + ‘B’ s2 = ‘221B’
s3 = ‘12’ + ‘45’ s3 = ‘1245’
You cannot add a number to a string. You have to convert a number to a string before you can add it to a string.
Examples: s4 = ‘Year’ + 2020 Will produce ERROR
s5 = ‘Year’ + ‘2020’ s5 = ‘Year2020’
s6 = ‘Year’ + str(2020) s5 = ‘Year2020’
String Replication: The * Operator is used to replicate a string side by side. It creates a New String by repeating a string.
Examples: s1 = ‘Ta’ * 4 s1 = ‘TaTaTaTa’
s2 = 3 * ‘Rat’ s2 = ‘RatRatRat’
s3 = ‘ABC’ * 0 s3 = "" (null string)
s4 = ‘12’ * 5 s4 = ‘1212121212’ (not 60)
s5 = ‘ABC’ * ‘PQR’ ERROR, as you cannot multiply 2 strings
4. Using Membership Operator:
There are two string membership operators in and not in. Operator in returns True if a substring is present in a string.
Operator not in returns True if a substring is NOT present in a string
Examples: ‘a’ in ‘India’ True (as ‘a’ is present in ‘India’)
‘rat’ in ‘rattle’ True (as ‘rat’ is present in ‘rattle’)
‘in’ in ‘beginning’ True (as ‘in’ is present in ‘beginning’)
‘M’ in ‘Sodium’ False (case does not match for ‘m’ and ‘M’)
‘bat’ not in ‘tribunal’ True (as ‘bat’ is not present in ‘tribunal’)
‘by’ not in ‘goodbye’ False (as ‘by’ is present in ‘goodbye’)
Examples: s = ‘therefore’
sub = ‘re’
sub in s True (here the values in the variables are compared)
Example: Program to count the number of vowels in a string.
The for loop extracts all the characters one by one from the input string word and
assigns them to the loop variable c. The character c is then compared with each of
the characters in the string ‘AEIOUaeiou’. If a match is found, then the condition
becomes True and the counter count is incremented by 1. The final count value is
printed outside the loop.
5. String Comparison Operators
Python compares strings using the dictionary order. The ASCII value of characters in a string is used to decide upon the order.
A → Z has ASCII values 65 → 90 and a → z has ASCII values 97 → 122.
Strings that will occur lower in the dictionary are given a lower value compared to strings that occur higher in the dictionary.
Hence CAPITAL letters are considered lesser than small letters. Accordingly, the string ‘Great’ is considered less than ‘great’.
Examples: print ( ‘top’ == ‘tap’ ) False
print ( ‘Bat' < ‘Mat' ) True
print ( ‘cattle' < 'catch’ ) False
print ( 'cat’ < 'catch’ ) True
print ( 'bat' < 'Battle' ) False
print ( ‘23' < ‘125' ) False
6. Use of ord() and chr() Functions:
• ord( ): This function is used to get the ASCII value of a character
• chr( ): This function is used to get the character for a given ASCII value
Examples showing the use of ord() and chr() functions.
Eg. To modify a string by replacing a character by another character which is 2 characters ahead of the given character.
The for loop extracts all the characters one by one from the input string word
and assigns them to the loop variable c. The ord(c) function then gets the ASCII
value of the character in c and adds 2 to it to get the ASCII value of the
character 2 characters ahead of the character in c. The chr() function then
reconverts the ASCII value to a character and adds it to the new string variable
newWord.
7. The use of String Slicing
Slice: A slice is a part of the string from the original string
Range: The slice is formed using a range of index. The slice starts from a starting index and takes all characters up to 1 less
than the ending index. The index used to get the range can be both +ve / -ve
Step: A third step parameter can be provided to skip values while choosing the elements of a slice. A positive step value skips
characters from left to right, while a negative step value skips characters from right to left of a string
Some examples of string slicing:
Note:
• When the starting index is not
given, it means index 0
• When the ending index is not
given, it means the length of the string
• When step value is not given, it means the default step value of +1
Working with In-Built String Manipulation Methods
▪ [Link](): Returns a copy of the string with its first character capitalized and all other characters
changed to lowercase
▪ [Link](): Returns a copy of the string with the first alphabet of each word in uppercase
▪ [Link](): Returns True if all characters in the string are white space characters (like newline \n, tab
\t or space). Else returns False. Returns False for a null string.
▪ [Link](): Returns True if all characters in the string are alphanumeric i.e. either alphabets or digits
or both. Else returns False.
▪ [Link](): Returns True if all characters in the string are alphabets only. Else returns False
▪ [Link](): Returns True if all characters in the string are digits only. Else returns False
▪ [Link](): Returns True if all characters in the string are in lowercase. Else returns False. At least one
character should be an alphabet for it to return True.
▪ [Link](): Returns True if all characters in the string are in
uppercase. Else returns False. At least one character should be an
alphabet for it to return True.
▪ [Link](): Returns a copy of the string with all alphabets
converted to lowercase
▪ [Link](): Returns a copy of the string with all alphabets
converted to uppercase
▪ [Link]( sub, start, end ): Returns the lowest index in the string, where the sub-string sub is found
within the slice range between start and end. If start and end are not given, then uses the entire string.
Returns -1 if sub is not found.
▪ [Link](): Counts the number of occurrences of a sub-string in a string.
▪ [Link](): Joins a string or a character in-between each member of a sequence.
▪ [Link](): Splits a string by white-space characters (space, newline, tab) when no splitting string is
specified (by default). Else it splits at the specified string. The result is a LIST containing the split string
parts. The splitting string is removed and is not present in the resultant list.
▪ [Link](): Partitions a string with respect to a given sub-string, into 3 parts always, to form a tuple.
In case the sub-string occurs more than once, ONLY the first occurrence is used. The three parts are the
part on the left of the partition, the partition string and the part on the right of the partition string.
▪ [Link](): Replaces all occurrences of a given string (first string) with another string (second string).
Replaces By ‘i’
‘ro’
▪ index( ): Returns the index of the first occurrence index of a substring in a string. If substring is not
present, it raises an error. It can be used with a string, list, tuple.
▪ startswith( sub, start, end ): Returns True if a string starts with a given character or sub-string within a
range. Else returns False.
▪ endswith( ): Returns True if a string ends with a given character or sub-string within a range. Else returns
False.
▪ lstrip( ): Removes all occurrences of a character or sub-string
from the left of a string only. In case no argument is
provided, all whitespace characters i.e. ‘ ‘, ‘\n’, ‘\t’ are
removed from the left of the string.
For [Link](‘create’), Python will one by one take a character
from the left of the string ‘terracotta’ and see if that character
is present in the string ‘create’ or not. If present, it will be removed from ‘terracotta’. The process stops
when a particular character from ‘terracotta’ is not present in ‘create’. Hence, ‘t’, ‘e’, ‘r’, ‘r’, ‘a’, ‘c’ are
removed from the left of ‘terracotta’ as these characters are present in the string ‘create’. The process
stops as the next character ‘o’ is not present in ‘create’
▪ rstrip( ): Removes all occurrences of a character / sub-string
from the right of a string only. In case no argument is
provided, all whitespace characters i.e. ‘ ‘, ‘\n’, ‘\t’ are
removed from the right of the string.
For [Link](‘create’), Python will one by one take a character
from the right of the string ‘terracotta’ and see if that
character is present in the string ‘create’ or not. If present, it will be removed from ‘terracotta’. The process
stops when a particular character from ‘terracotta’ is not present in ‘create’. Hence, ‘a’, ‘t’, ‘t’, are removed
from the right of ‘terracotta’ as these characters are present in the string ‘create’. The process stops as the
next character ‘o’ is not present in ‘create’.
▪ strip( ): Removes all occurrences of a character or sub-string
from both ends of a string. In case no argument is provided, all
whitespace characters i.e. ‘ ‘, ‘\n’, ‘\t’ are removed from the
right of the string
For [Link](‘create’), Python will first take each character from
the left of the string ‘terracotta’ and see if that character is
present in the string ‘create’ or not. If present, it will be removed from ‘terracotta’. The process stops when
a particular character from ‘terracotta’ is not present in ‘create’. Hence, ‘t’, ‘e’, ‘r’, ‘r’, ‘a’, ‘c’ are removed
from the left of ‘terracotta’ first, as these characters are present in the string ‘create’. The left side process
stops as the next character ‘o’ is not present in ‘create’.
Next, Python will one by one take a character from the right of the string ‘terracotta’ and see if that
character is present in the string ‘create’ or not. If present, it will be removed from ‘terracotta’. The process
stops when a particular character from ‘terracotta’ is not present in ‘create’. Hence, ‘a’, ‘t’, ‘t’, are removed
from the right of ‘terracotta’ as these characters are present in the string ‘create’. The process stops as the
next character ‘o’ is not present in ‘create’.
The final character left after the characters are removed from both the ends of ‘terracotta’ is ‘o’.
Problems:
1. Program to input a sentence and find how many number of words are present in the sentence,
2. Write a program to input a sentence and find how many times the letter ‘a’ or ‘A’ occurs in the string.
3. Input a sentence and count:
a) Number of alphabets
b) Number of digits
c) Number of spaces
d) Number of uppercase letters
e) Number of lowercase letters
f) Any other remaining characters
4. Input a sentence and count the number of vowels in the sentence.
5. Program to input a word and check if it is a palindrome or not
6. Write a program to find how many times the word ‘HE’ occurs in a sentence. Word can be present as “HE,
‘He’, ‘he’, or ‘hE’.
7. Input a sentence and print all the words that start with ‘T’ or ‘t’ and also print a total count of all those words.
8. Write a program to input a sentence and capitalize the last letter of every word in a sentence.
9. Write a program to input a sentence and find how many words end with ‘e’ or ‘E’. Also print those words.
10. Write a program to input a sentence and print all the locations of the sub-string ‘th’ in the sentence.
p p p p
11. Find the output of the following code
12. Find the output of the following code