PF Lab 12
PF Lab 12
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
Output:
First string convert in list and separated all words of string in list and in last use
print function.
Correct code:
a =['P','Y','T','H','O','N']
a[0] = "x"
#Apply Exception for mention error
print(a)
OUTPUT:
Page 1|6
Programming Fundamentals (SE-102L) SSUET/QR/114
2. Code:
a = STRING
i=0
while i < len(b):
c = a[i]
print(c)
i+=i + 1
Output:
First STRING converted in string by inverted common and len(b) converted in
len(a) because string assign in a variable and we will removed + from i variable.
Correct code:
a = 'STRING'
i=0
while i < len(a):
c = a[i]
print(c)
i=i + 1
OUTPUT:
Page 2|6
Programming Fundamentals (SE-102L) SSUET/QR/114
3. Code:
Def 1my_function(x):
return x[::-1]
mytxt = 1my_function("I wonder how this text looks like backwards")
print(mytxt)
Output:
First Def converted in def and 1 removed from my-function(x) and same
processed in other function name.
Correct code:
def my_function(x):
return x[::-1]
mytxt = my_function("I wonder how this text looks like backwards")
print(mytxt)
OUTPUT:
Page 3|6
Programming Fundamentals (SE-102L) SSUET/QR/114
2. Code:
s = input("Enter a string: ")
if "Python" in s:
print("Python", "is in", s)
else:
print("Python", "is not in", s)
Output:
3. Code:
str='cold'
list_enumerate=list(enumerate(str))
print("list enumerate:", list_enumerate)
Page 4|6
Programming Fundamentals (SE-102L) SSUET/QR/114
Page 5|6
Programming Fundamentals (SE-102L) SSUET/QR/114
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
SOURCE CODE:
c=input("Enter your favorite color:")
print(10*c)
print(c," "*10,c)
print(10*c)
OUTPUT:
Page 6|6