You are on page 1of 9

Department of Software Engineering

CS-114: Fundamentals of Programming


Class: BESE-12B
Lab 08: Strings and Lists-II
CLO 4: Adopt the usage of latest IDEs and other supplementary tools to aid implementation and code
management

Date: December 18th, 2022

Time: 2:00-5:00

Instructor: Dr Zuhair Zafar

Lab Engineer: Mr. Nadeem Nawaz


Lab 08: Strings and Lists-II

Objectives
In this lab the students will learn and practice to develop the understanding of strings and lists
in Python.

Tools/Software Requirement
Python IDLE

Description
Follow the lab manual step by step. As you proceed, you will be asked to add screenshots/snaps
of your results inside the provided output windows within this manual for grading purposes.

Strings
Strings can be concatenated by using + operator. The concatenation can be done on more than
two strings. For example:
string1=”hello”
string2= string1+“how are ”+”you”

You can repeat a string a certain number of times by using the product operator. For example:
print(“hello”*5)

The length of a string can be found by using the len function. For example:
print(len(string1))

To access a specific index in a string, we write the index in square brackets. For example:
s = “abc”
s[0]=“a” s[1]=“b” s[2]=“c” s[-1]=“c” s[-2]=“b”
Strings can be sliced in the following manner:
s=“abcdefgh”
s[3:6]=“def” s[3:6:2]=“df” s[::]=“abcdefgh”
s[::-1]=“hgfedcba s[4:1:-2]=“ec”

We cannot modify a particular index in a string. For example:


s = “hello”

s[0]=“y” # this assignment would give an error

The following code can be used to modify an element of a string:


s=“y” + s[1:len(s)]

Strings are iterable. The following code can be used to iterate through the characters of a
string:
for char in string1:
print(char)

Lists
An empty list can be defined as follows:
mylist=[]

The following code can be used to initialise a list of n elements with zeros:
mylist=[0]*n

To find the length of a list we can use the len function. Unlike strings, a certain index of a list
can be modified.

We can iterate through the elements of a list by using the following code:
for i in mylist:
print(i)
We can add elements to the end of a list with L.append(element). For example:
L=[1,2,3]

L.append(5) # L is now [1,2,3,5]

We can concatenate lists by using + operator. For example:


L1=[1,2,3]
L2=[4,5,6]

L3=L1+L2 # L3 is [1,2,3,4,5,6]

A list can be extended by using the following code:


L1=[1,2,3]

L1.extend([4,5,6]) # L1 changes to [1,2,3,4,5,6]

We can delete element at a specific index in a list by using del(L[index]). For example:
L=[2,1,3,6,3,7,0]

del(L[1]) # L=[2,3,6,3,7,0]

We can remove element at the end of list with L.pop(). This function returns the removed
element. For example:
L=[2,1,3,6,3,7,0]

L.pop() # L=[2,1,3,6,3,7] and returns 0

L.pop(3) # L=[2,1,3,3,7,0] and returns 6

We can remove a specific element with L.remove(element). For example:


L=[2,1,3,6,3,7,0]

L.remove(2) # L=[2,1,6,3,7,0]

L.remove(3) # L=[2,1,6,7,0]

L.remove(9) # gives an error


We can sort a list in two ways:
L1=[9,6,0,3]

L2=sorted(L1) # L2=[0,3,6,9]

L1.sort() # L1 =[0,3,6,9]

We can reverse a list by using the following code:


L=[11,15,13,10]

L.reverse() # L=[10,13,15,11]

Conversion of string to list and vice versa


Strings can be converted to lists with list(s). For example:

s=“hello”

L=list(s) # L=[‘h’,‘e’,‘l’,‘l’,‘o’]

We can split a string by using s.split(). For example

s=“hello how are you”

a=s.split() # a=[‘hello’,’how’,’are’,’you’]

s=“hello-how-are-you”

a=s.split(“-”) # a=[‘hello’,’how’,’are’,’you’]

To convert a list of characters in to a string we use ‘’.join(L)

L=[‘a’,’b’,’c’]

s=‘’.join(L) # s=“abc”

s=‘-’.join(L) # s=“a-b-c”

Submission Deadline

The submission deadline for this lab task is 23:59 hrs on 18 jan 2022.
Lab Tasks:
Using only the programming techniques that you have learned so far in the lectures, perform
the following tasks.

Note: All the tasks of this lab should be performed in Python scripted mode only. You should
use sensible and self-explanatory names for the variables you are using. Please add comments
to your code to explain what you are doing. Also use print statements to explain your program
as well as inputs and outputs.

1. Write a python program that takes two lists as input from the user and displays the
common elements of those lists.

Example program:

Please input the first list with elements separated by commas: 5,2,”a”,75,”hello”
Please input the second list with elements separated by commas: 9,11,”59”,5 ,”a”,”how”,3
The common elements are: 5,”a”

Solution:
2. Write a python program that takes a list of integers as input and then sorts it in
ascending order and displays the sorted list. Do not use inbuilt sort and sorted methods
for lists.

Example program:

Please enter a list of integers separated by a comma: 13,10,25,15,21


The sorted list in ascending order is: [10,13,15,21,25]

Solution:
3. Write a python program that takes a paragraph in English as input and finds and displays
the word with the largest number of characters.

Example program:

Please enter the paragraph: “Python is an interpreted high level, general purpose
programming language. Its design philosophy emphasizes code readability with its use of
significant indentation. Its language constructs as well as its object oriented approach aim to
help programmers write clear, logical code for small and large scale projects.”
The words with the largest number of characters are:
interpreted
programming
readability
Solution:
significant
indentation
programmers

You might also like