You are on page 1of 6

USAMA MEHBOOB ROLL NO 2022F-SE-225

SECTION E
LAB # 12
STRINGS
OBJECTIVE:
Working on the strings formatting.

EXERCISE:
A. Point out the errors, if any, and paste the output also in the following
Python programs.
1. CODE:
a = “Python”
a[0] = “x”
#Apply Exception for mention error
ERROR:
In line 2 b=a.replace is adding

Also print line 2

CORRECT CODE:

OUTPUT:
USAMA MEHBOOB ROLL NO 2022F-SE-225
SECTION E
2. CODE:
a = STRING
i=0
while i < len (b):
c = a[i]
print(c)
i+=i + 1
ERROR:
Inverted Qoma’s on string

len(b) in line 3

CORRECT CODE:

OUTPUT:
USAMA MEHBOOB ROLL NO 2022F-SE-225
SECTION E
3. CODE:
Def 1my_function(x):
return x[: :-1]

mytxt = 1my_function(“I wonder how this looks like backwards”)


print(mytxt)
ERROR:
D is capital and variable not start with no 1my

Same in line 3 1my

CORRECT CODE:

OUTPUT:

B. What would be the output of the following programs:


1. CODE:

OUTPUT:
USAMA MEHBOOB ROLL NO 2022F-SE-225
SECTION E
2. CODE:

OUTPUT:

3. CODE:

OUTPUT:
USAMA MEHBOOB ROLL NO 2022F-SE-225
SECTION E
C. Write Python programs for the following:
1. Write a program that Store a person’s name, and include some whitespace characters
at the beginning and end of the name. Make sure you use each character combination,
"\t" and "\n", at least once. Print the name once, so the whitespace around the name is
displayed. Then print the name using each of the three stripping functions, lstrip(),rstrip(),
and strip().
CODE:

OUTPUT:
USAMA MEHBOOB ROLL NO 2022F-SE-225
SECTION E

2. Write a program that asks the user for their favourite color. Create the following output
(assuming blue is the chosen color)(hint: use ‘+’ and ‘*’)
blueblueblueblueblueblueblueblueblueblue
blue blue
blueblueblueblueblueblueblueblueblueblue

CODE:

OUTPUT:

You might also like