Course- BTech Type- Core
Course Code-CSET101 Course Name- Computational
Thinking and Programming
Year-2023 Semester-odd
Date- Batch- ALL
Tutorial Assignment: 7 Solution
String operations -
txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))
txt = "Hello, welcome to my world."
x = txt.find("welcome")
print(x)
txt = "Mi casa, su casa."
x = txt.rfind("casa")
print(x)
txt = "apple, banana, cherry"
x = txt.rsplit(", ")
print(x)
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
txt = "Hello my FRIENDS"
x = txt.lower()
print(x)
txt = "Hello my friends"
x = txt.upper()
print(x)
isalpha()
isdigit()
islower()
isupper()
isspace()
strip(), lstrip(), rstrip()
Regular expressions
A RegEx, or Regular Expression, is a sequence of characters that forms a search
pattern. RegEx can be used to check if a string contains the specified search pattern.
import re
findall Returns a list containing all matches
search Returns a Match object if there is a match anywhere in the string
split Returns a list where the string has been split at each match
sub Replaces one or many matches with a string
txt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)
txt = "The rain in Spain"
x = re.search("\s", txt)
print("The first white-space character is located in position:", x.start())
txt = "The rain in Spain"
x = re.split("\s", txt)
print(x)
txt = "The rain in Spain"
x = re.sub("\s", "9", txt, 2)
print(x)
EXCEPTION HANDLING
The try block lets you test a block of code for errors.
The except block lets you handle the error.
The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the result of the try- and
except blocks.
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
try:
f = open("demofile.txt")
try:
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
except:
print("Something went wrong when opening the file")
ANSWERS
Q1.
word1[i] = space
TypeError: 'str' object does not support item assignment
String is an immutable object
Q2.
word = input()
word1 = list(word)
no = len(word)
space = '/'
for i in range(no):
if word[i] == " ":
word1[i] = space
word1 = ''.join(word1)
print(word1)
Q3.
print(" ".join[l])
TypeError: 'builtin_function_or_method' object is not subscriptable
Q4.
b)
string = "this university is best for CS aspirants"
s = string.split()[::-1]
l = []
for i in s:
l.append(i)
print(" ".join(l))
Q5.
b) AzbycX (starts with space)
s1 = "Abc"
s2 = "Xyz"
s1_l = len(s1)
s2_l = len(s2)
length = s1_l if s1_l>s2_l else s2_l
result = " "
s2 =s2[::-1]
for i in range(length):
if i<s1_l:
result = result + s1[i]
if i<s2_l:
result = result + s2[i]
print(result)
Q6.
Yes!We have a match
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$",txt)
if x:
print("found")
else:
print("not found")
Q7.
['ai', 'ai']
[]
['The', 'rain', 'in', 'Spain']
import re
txt = "The rain in Spain"
x = re.findall("ai",txt)
print(x)
x = re.findall("portugal", txt)
print(x)
x = re.search("\s", txt)
print(x.start())
x = re.split("\s", txt)
print(x)
Q8.
['The', 'rain in Spain']
The9rain9in9Spain
The9rain9in Spain
<re.Match object; span=(5, 7), match='ai'>
import re
txt = "The rain in Spain"
x = re.split("\s", txt,1)
print(x)
x = re.sub("\s","9",txt)
print(x)
x = re.sub("\s", "9", txt,2)
print(x)
x = re.search("ai",txt)
print(x)
Q9.
print(x.string())
TypeError: 'str' object is not callable
Q10.
(12, 17)
Spain
txt = "The rain in Spain"
x = re.search(r"\bS\w+",txt)
print(x.span())
print(x.group(0))
Q11.
b) Syntax error
print(a/b)
NameError: name 'a' is not defined
def fn(a,b):
try:
print(a/b)
except TypeError:
print("unsupported operation")
except ZeroDivisionError:
print("Division by zero not allowed")
fn(0,0)
Q12.
A parameter is the variable listed inside the parentheses in the function
definition. An argument is the value that are sent to the function when it
is called.
Q13.
my_fuction("Emili")
TypeError: my_fuction() missing 1 required positional argument: 'lname'
def my_fuction(fname, lname):
print(fname+" "+lname)
my_fuction("Emili","name")
Q14.
d)
if 5>2:
print("Five is greater than two!")
if 5>2:
print("Five is greater than two!")
Q15.
d = max(a,b.c)
AttributeError: 'int' object has no attribute 'c'
def max(x,y,z):
if x>y:
if x>z:
return x
else:
return z
else:
if y>z:
return y
else:
return z
all_list = [int(m) for m in input().split()]
a = all_list[0]
b = all_list[1]
c = all_list[2]
d = max(a,b,c)
print("maximum is",d)