0% found this document useful (0 votes)
9 views6 pages

PF Lab 12

The document outlines a lab exercise for Programming Fundamentals focusing on string manipulation in Python. It includes identifying and correcting errors in provided code snippets, predicting outputs of specific code segments, and writing Python programs for user input and string formatting. The exercises emphasize understanding string operations and exception handling.

Uploaded by

Rameen Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

PF Lab 12

The document outlines a lab exercise for Programming Fundamentals focusing on string manipulation in Python. It includes identifying and correcting errors in provided code snippets, predicting outputs of specific code segments, and writing Python programs for user input and string formatting. The exercises emphasize understanding string operations and exception handling.

Uploaded by

Rameen Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Fundamentals (SE-102L) SSUET/QR/114

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

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


1. Code:
s= "Welcome"
for i in range(0, len(s), 2):
print(s[i], end = '')
Output:

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

print("list length:", len(str))


s1 = "Welcome to Python"
s2 = s1.replace("o","abc")
print(s2)
a = "Python" + "String"
b = "<" + a*3 + ">"
print(b)
Output:

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().
SOURCE CODE:
name = " \tHaris Sardar\n "
print(name.lstrip())
print(name.rstrip())
print(name.strip())
Output:

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

You might also like