String manipulation
UNIT PLAN
● You will learn about String Manipulation in Python
Why are you headed there?
● Learning about String data type is a fundamental requirement for developing coding skills
In what ways will you be assessed?
● You will write programs and complete a worksheet.
Plan for this hour:
Introduction to strings -
a. Creating strings
b. String indexing
c. Accessing strings
d. String traversal
2
What are strings?
A string is a sequence, which means that the elements of the string (which are also called characters in a
string) occupy contiguous memory spaces and we can access these elements using an index.
How can strings be created?
● Using single quotes to the value
● Using double quotes to the value
● Using str() - this function will take any value and try and convert it into a string. However, if we are
directly trying to add a string inside, it should still be enclosed within quotes.
Accessing elements of a string
Elements of a string can be accessed using square brackets independently as well as using a loop and traversing through
the string.
The index of a sequence is as an integer that denotes the position of a character in the [Link] indexing
always starts from 0 to n-1 where n is the length of the string.
Strings can also have negative indexing.
Index is an integer that denotes the position of a character in the string.
Example:
s = ‘sky’
● Index starts at 0 and increments by one for the next index. Character s k y
● Characters of a string can be accessed using its index:
index 0 1 2
● The first index is 0.
● We access a character by placing the index inside square brackets.
● The last index is the length of the string - 1. It is one less because the index starts from 0.
Traversal of a string
Traversal means to visit each element of the string. This is done using a loop.
METHOD 1:
METHOD 2:
Here, len() is used to determine the length of a sequence.
The length value can be used in a for loop to access each element of a sequence
along with the index value.
Important points:
● So far we have seen forward or positive indexing in the examples, that is, the indices move in incremental order
from 0 to length-1.
● Python also allows negative indexing in sequences.
● In negative indexing, the last character has an index -1 and then it decrements by one for every character before it.
● The very first character of the string ends up having a -length index.
Example:
s = “PYTHON”
Length of s is 6
● The positive indexing starts at 0 and goes upto 5.
● The negative indexing starting from the beginning of the string is -6 and proceeds to -1.
Exit ticket-Try these out:
Create these strings:
(i) a = 2, b = 5 create a string of the result that is obtained from this operation: ab > ba
(ii) Convert the third prime number in the range 100 to 120 into a string
(iii) Create a string with the words, ”string builder”.
Assignment 1
Write solutions to this program:
In class assignment - 1
Accept a string and print the characters of the string on the same line (i) in the same order (ii) in reverse order.
Check if a given string is a palindrome.
Plan
● Mutability of strings
● String Operators
○ Concatenation Operator
○ Replication Operator
○ Membership Operator
○ Comparison Operator
Quick revision
What do we mean by mutability?
List the mutable and immutable data types.
Mutable-List, dictionary, set
Immutable-int, float, decimal, bool, string, tuple
Strings are immutable
● String data type is immutable.
● This means characters of a string cannot be replaced in the same string.
● A new string needs to be created if elements have to be replaced.
For example:
Concatenation Operator
● ‘+’ Is known as the concatenation operator.
● Strings can be merged / joined using concatenation (+) operator.
Difference between using = and using comma separator in a print function.
In the statement: s1 = s1 + s2, a new string s1 is created using the old string s1 and the id of old s1
and new s1 are different.
Important:
For concatenation to work, both the inputs have to be string.
● If both inputs are numbers, then the default addition takes place. A number and a string
cannot be added as neither addition nor concatenation is feasible.
● Variable assignment is not affected by a variable being mutable or otherwise, however,
element-wise assignment is not possible in immutable data types.
Replication operator
● A replication operator repeats the character that many times.
● A replication operator takes a string and an integer only as inputs.
● Example - Generating patterns without the use of nested loop used replication operator.
● When used with operands of number data type, replication operator performs multiplication.
● If a number and a string is given, then replication takes place.
● If the operator is tried with two strings, then the operation raises an error.
Membership operator
Using membership operator in traversing a sequence:
Ordinal value
● Ordinal value can be checked using the ord() function.
● Ordinal value is nothing but the ASCII value of the character.
Ordinal numbers can be used to retrieve the character using chr().
Comparison operators
Comparison operators in Python: >, <, >=, <=, ==, !=.
● Logic can be used to compare number values.
● While comparing other data types, lexicographical comparison is done by Python.
● Lexicography is based on dictionary-like order.
● In the above example, ‘r’ and ‘b’ are compared and ‘r’ is found to be greater than ‘b’. Hence, comparison
stops there and s1 is considered greater than s2.
● ‘r’ is greater than ‘b’ as it has a higher ordinal value.
● Python compares using Unicode values(called ordinal value)
● Python compares two strings through relational operators using
character-by-character comparison of their unicode values.
Characters Ordinal Values
‘0’ to ‘9’ 48 to 57
‘A’ to ‘Z’ 65 to 90
‘a’ to ‘z’ 97 to 122
Post work
DO NOW:
print(‘=’ * 30)
print(‘\n’ * 9)
print(‘=’ * 30)
In class Assignment - 2
Students write program solutions for these questions:
1. Create the string, ‘12345678910’ using a loop and concatenation operator.
2. Create the given pattern using replication operator instead of nested loop:
#
##
###
####
SOLUTION - Assignment 2
Solutions:
1.
# create string 12345678910
s = ''
for i in range(1,11):
temp = str(i)
s += temp
print(s)
2.
# creating a right angled triangle pattern
n = int(input('Enter the number of lines in the pattern '))
s = '# '
for i in range(1,n+1):
print(s*i)
Questions for assignment 3:
1. Out of the following operators, which ones can be used with strings?
=, -, *, /, //, %, >, <>, in, not in, <=
1. What is the result of the following statement, if the input is ‘fun’?
print(input(“Enter a word “) + “trial” + “Ooty”*3)
1. Which of the following is not a legal Python string operation?
a. ‘abc’ + ‘abc’ b. ‘abc’ *3 c. ‘abc’ + 3 d. ‘abc’.lower()
1. Can we consider strings as character lists in Python? Why / Why not?
2. Question 1 in this image.
1. Question 2A and 2B in this image.
7. Question 5 in this image.
Solutions to assignment 2
Ans 1
s=''
for i in range(1,11):
s=s+str(i)
print(s)
Ans2:
for i in range(4):
print('#'*(i+1))
Answers to Assignment 3
1.=,*,in,not in, >,<=
[Link]
3 option c->‘abc’ + 3
[Link], as lists are mutable but strings are not.
[Link] EOL while scanning string literal
5b
Test.
Next line.
5c.
One Two Two
OneTwoTwo
10
6. (i) a (ii)c (iii)d
(iv)d
(v)b ->other int would be larger
Plan
String slices
String slices
● Slice means a part of.
● A string slice is a part of a string containing some contiguous characters from the string.
● nameofstring[n:m]
● n and m are integers and legal [Link] displays a slice of a string by returning the characters falling between n and
m.
That is n,n+1,n+2….till m-1
Example:
word=’amazing’
print(word[0:4])------>will print ‘amaz’
word[:7] —--> will print ‘amazing’
word[:3]------> will print ‘zing’
word[5:] —--->will print ‘ng’
Important points:
● Slice operation accepts 3 arguments: [start:end:step].
The syntax for using the slice operator is as follows:
S2 = S1[start:end:step]
● Similar to the range function, if a value is not entered for start, then Python slices the string starting from the beginning of the string.
S1 = ‘red jacket’
S2 = S1[ : 3 : 1]
print(S2) displays red
● If the end is not mentioned, then Python assumes that the slice needs to be done until the end of the string.
S1 = ‘red jacket’
S2 = S1[ 4 : : 1 ]
print(S2) displays jacket
● If the step is not given, then a default step size of 1 is used.
S1 = ‘red jacket’
S2 = S1[ : 3]
print(S2) displays red because step is assumed to be 1
In the example below, start is 4 and step is 2
S1 = ‘red jacket’
S2 = S1[4 : : 2]
print(S2) displays jce
Important points
● If all three arguments are not given, then the entire string is recreated as a slice.
S1 = ‘red jacket’
S2 = S1[ : : ]
print(S2) displays red jacket
● While the values may be skipped, there should be at least one colon as a separator for Python to understand that we are trying to slice the
string.
>>> S1 = 'red jacket'
>>> S2 = S1[]
SyntaxError: invalid syntax
>>> S2 = S1[:]
>>> print(S2)
red jacket
● Additional examples:
● A slice of a string returns another string, a slice of a list returns a list. So a slice returns another sequence of the same type.
Important points
● A new reversed sequence can be returned using: s[: : -1]
>>> S1 = 'red jacket'
>>> S1[::-1]
'tekcaj der'
● Slice ranges need not be a legal range (permissible indices)
If the lower range is out of bounds, then the lower range is considered as 0 for range in forward direction and -length in slice is assumed in
reverse direction.
S[-1:-12] returns null string as step size is positive 1 by default and with this step size, it would not be possible to start at -1 and end at -11.
Assignment 4-do now
Complete the Checkpoint 5.1 or 10.1 (pg 192 in old edition and pg 344 in new edition)for this topic in the textbook.
Assignment 4 solutions
[Link] stored in contiguous memory locations
2. ‘G’ and ’ y’
3. True 9. Slice means a part of.A
4. (1)Concatenation (2)replication
string slice is a part of a
string containing some
[Link] Knights who say,ni!
contiguous characters from
5b. Spamspamspamni!ni!
5c. ’P’ the string.
6 in and not [Link] for the presence of the given
element in a [Link] true or false based on 10
presence or absence ‘pa’
Explain with example. ‘ani’
[Link] character by character in lexicographical ‘spam!’
order(based on ASCII valueS) ‘spam’
8. ‘ni!’
’o’
‘g’
7
‘Myst’
False
False
True
True
False
Assignment 5
Complete the following questions:
Assignment 5 solutions
Plan
● Answers to questions.
● Builtin String Functions - isalpha(), isdigit(), islower(), isupper(), isspace(), count(),
index()
● Assignment on string slices
String functions
1. isalpha()
2. isdigit()
3. islower()
4. isupper()
5. isspace()
6. count()
7. index().
The string builtin function always accepts a string as input (some may accept additional arguments).
note:
For functions in points 1 to 5 please refer the textbook.(old edition pg 193,new edition pg 345)
count()
● The count() is a built-in function in Python. It will return the total count of a given element in a string.
● The counting begins from the start of the string till the end.
● It is also possible to specify the start and end index from where you want the search to begin.
Syntax:
[Link](char or substring, startindex, endindex)
Refer this link for further reading:
[Link]
count()
Syntax:
[Link](char or substring, startindex, endindex)
Parameters of Python Syntax
● Char or substring: You can specify a single character or substring you are wants to search in the given string. It will
return you the count of the character or substring in the given string.
● start : (optional) It indicates the start index from where the search will begin. If not given, it will start from 0. For
example, you want to search for a character from the middle of the string. You can give the start value to your count
function.
● end: (optional) It indicates the end index where the search ends. If not given, it will search till the end of the list or
string given. For example, you don't want to scan the entire string and limit the search till a specific point you can
give the value to end in your count function, and the count will take care of searching till that point.
● End value/Upper limit is not included in the search.
ReturnValue
The count() method will return an integer value, i.e., the count of the given element from the given string.
It returns a 0 if the value is not found in the given string.
index()
● The index() method finds the first occurrence of the specified value.
● Using the index() if the value is not found, the index() method will raise an exception:ValueError: substring
not found
[Link](value, startindex, endindex)
● value-Required. The value to search for
● start-Optional. Where to start the search. Default is 0
● end-Optional. Where to end the search. Default is to the end of the string.
● End value/Upper limit is not included.
Return value:The index() method will return an integer value, i.e., the starting index of the given string from the
main string. It shows exception if the value is not found in the given string.
Refer this link:[Link]
Assignment-6
Write and execute solutions for the following program:
Write a program that reads a string and displays all substrings of the given string having consecutive consonants
Ex: “She sells sea shells” - should yield: ‘Sh’, ‘ s’, ‘lls s’, ‘ sh’, ‘lls.
Hint:
The problem can be decomposed in this manner:
● What should be the input?
● Will we need a loop to enable the checking? Would it be a for loop or a while loop?
● What should be the condition to find out the consecutive consonant string?
● How will we display these consecutive consonant strings?
Plan
● Discussion to assignment 6 question
● String functions - capitalize(), lower(), upper()
● Practice Programs on string functions.
Sample solution 1
Sample solution 2
Refer book for explanation(pg 193 in old edition and 345 in new )
capitalize()
lower()
upper()
In class assignment -7 and 8 combined
Write and execute program solution for these questions:
● Accept a string and print the number of alphabets, number of uppercase alphabets, number of
lowercase alphabets, digits and special characters in the string.
● Write a program that accepts a string and capitalizes the first character of each word present in the
string and prints both the original string as well as the capitalized string.
● Write a program that reads a string and toggles the case of all the alphabets in the string.
Solutions:
‘’’Accept a string and print the number of alphabets, number of uppercase alphabets, number of lowercase
alphabets, digits and special characters in the string.‘’’
str = "ABC123#@*^456wxyz"
upper, lower, number, special = 0, 0, 0, 0
for i in range(len(str)):
if str[i].isupper():
upper += 1
elif str[i].islower():
lower += 1
elif str[i].isdigit():
number += 1
else:
special += 1
print('Upper case letters:', upper)
print('Lower case letters:', lower)
print('Number:', number)
print('Special characters:', special)
Write a program that accepts a string and capitalizes the first character
of each word present in the string and prints both the original string
as well as the capitalized string.
sample_text = "today is Thursday"
original=sample_text
result = ""
# Split the string and get all words in a list
list_of_words = sample_text.split()
# Iterate over all elements in list
for elem in list_of_words:
# capitalize first letter of each word and add to a string
if len(result) > 0:
result = result + " " + [Link]().capitalize()
else:
result = [Link]()
print(original)
print(result)
Write a program that reads a string and toggles the case of
all the alphabets in the string.
str2=''
str = "Hello World";
for i in range(len(str)):
c=str[i]
if [Link]():
str2= str2+[Link]()
elif [Link]():
str2 =str2+[Link]()
else:
str2=str2+c
print(str)
print(str2)
Plan
● String functions - find(), strip(), lstrip(), rstrip()-please refer textbook for more reading
find()
find(sub,start, end).
-1 will be returned in case there is no match and if the substring is found it returns the index of its first occurrence(starting
lowest index where substring is found).
Refer textbook for more examples.
Refer this link:[Link] for more reading
index() vs find()
● The index() method is almost the same as the find() method, the only difference is that the find() method returns -1 if
the value is not found, but the index() method will raise an exception:ValueError: substring not found
strip(), lstrip() and rstrip()
Refer this link for strip() working :[Link]
Refer textbook examples for lstrip() and rstrip()(pg 348-353)
<string>.isalnum() - It returns True if the characters in the string are alphanumeric
and there is atleast one character, False otherwise. Space (‘ ‘) is not treated as
alphanumeric.
Syntax: <string>.isalnum()
”abc123”.islanum()
>> True
“123”.isalnum()
>>True
<string>.join(<string iterable>) - It joins a string or character after each member of
the string iterator i.e string based sequence.
● <string>.isalpha() - It returns True if all characters are alphanumeric( alphabets
and numbers) and there is at least one character or false otherwise.
● <string>.isspace() - It returns True if there are only whitespace characters in
the string.
● <string>.startswith() - It returns True if the string starts with the substring sub,
otherwise false.
● <string>.endswith() -It returns True if the string ends with the substring sub,
otherwise false.
● <string>.istitle() - It returns True if the string has title case.
● <string>.replace(old,new) - It returns a copy of the string with all occurrences
of substring old replaced by new string.
Plan
● String functions - partition() and split()
Partition()
The partition() method splits the string at the first occurrence of the argument string passed as separator
and returns a tuple containing the part the before separator, argument string and the part after the
separator.
The partition() method takes a string parameter separator that separates the string at the first occurrence
of it.
Return value:
The partition method returns a 3 element tuple containing the following:
● if the separator parameter is found in the string,it returns the part before the separator, separator
parameter/string, and the part after the separator
● If the separator parameter is not found,it returns the string itself and two empty strings.
split()
The split() method breaks up a string at the specified separator and returns a list of strings.
split() method takes a maximum of 2 parameters:
● separator (optional)- It is a delimiter. The string splits at the specified separator.
If the separator is not specified, any whitespace (space, newline etc.) string is a separator.
● maxsplit (optional) - The maxsplit defines the maximum number of splits.
The default value of maxsplit is -1, meaning, no limit on the number of splits.
Return value of split():
split() breaks the string at the separator and returns a list of strings.
Difference between split() and partition()
1. partition() just splits the string into two parts, given the [Link] splits exactly into two parts (left
part and right part of the specified delimiter).
While split() breaks it into multiple parts.
1. The output of partition()returns a tuple of exactly 3 elements: the left part, the delimiter, and the right
part.
While in split() output is a list of strings which have been split on the basis of separator .
1. Partition() takes maximum one argument,split() takes maximum two arguments.
Assignment 9
1. Write a program that reads a string and a substring. It should then display the number of
occurrences of the given substring in the string.
POST WORK
Complete prip book questions for this unit.
There are Assignments to be solved in the next class.
Assignment 10
1. Write a program that reads a string and capitalizes every third word of the string.
2. Write a program that reads a string and replaces all whitespaces with %.
Solutions
Write a program that reads a string and a substring. It should then display the number of occurrences of
the given substring in the string.
str1 = input("enter a string")
substr=input("enter a substring that you want to search")
print("The given substring is present",[Link](substr),"no of times" )
Write a program that reads a string and replaces all
whitespaces with %.
str2=''
str = input(“enter a string”)
for i in range(len(str)):
c=str[i]
if [Link]():
str2= str2+'%'
else:
str2=str2+c
print(str)
print(str2)
Plan
Lab record exercises Q 14,15 and 16
Lab record program link
Solution to Q14 of lab record:
s2=''
n=int(input("enter an integer"))
s=input("enter a string with/without numbers")
for i in s:
if [Link]():
s2=s2+i
if(s2==''):
print(n)
else:
res=n+int(s2)
print(res)
Revision
Do Now Activity
[Link] the errors. Find the line numbers causing errors.
S = "PURA VIDA"
S1 = S[: 10] +S[10 :]
S2 = S[10] + S[-10]
[Link] is the output produced?
>>> "-".join(['123','365','1319'])
>>> " ".join(['Python', 'is', 'fun'])
[Link] a string S, write expressions to print
first five characters of S
Ninth character of S
reversed S
alternate characters from reversed S
3-2-1
● 3 Things you learnt in String manipulation and use of string
built-in methods.
● 2 question you still have.
● 1 thing you enjoyed