0% found this document useful (0 votes)
10 views4 pages

Expt - 6 - To 7

Uploaded by

avimane0510
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)
10 views4 pages

Expt - 6 - To 7

Uploaded by

avimane0510
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/ 4

TY-BTECH-ETC ETP348 Python Programming Lab

---------------------------------------------------------------------------------------------------------------------------------------------------
Experiment No. – 06
CO Mapped : CO2 Level: L3
 Title: - Experiment on string manipulation in python
 Objectives:

 To understand the string manipulation operation


 Theory:
Strings are amongst the most popular types in Python. We can create them simply by
enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating
strings is as simple as assigning a value to a variable.
For example −
var1 = 'Hello World!'
var2 = "Python Programming"

 Accessing Values in Strings


Python does not support a character type; these are treated as strings of length one, thus
also considered a substring. To access substrings, use the square brackets for slicing along with
the index or indices to obtain your substring.
For example −
var1 = 'Hello World!'
var2 = "Python Programming"
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
When the above code is executed, it produces the following result −
var1[0]: H
var2[1:5]: ytho
 Updating Strings
You can "update" an existing string by reassigning a variable to another string. The new value
can be related to its previous value or to a completely different string altogether.
For example −
var1 = 'Hello World!'
print "Updated String :- ", var1[:6] + 'Python'
When the above code is executed, it produces the following result −
Updated String :- Hello Python

Department of Electronics and Telecommunication Engineering [D.K.T.E’s TEI,Ich] 14


TY-BTECH-ETC ETP348 Python Programming Lab
---------------------------------------------------------------------------------------------------------------------------------------------------
 Program Statement :
1. Write a python program to reverse the given string
2. Write a python program to check if a substring is present in a given string
3. Write a Python program to count the vowels present in the given string
4. Write a Python program to get a single string from two given strings, separated by a
space and swap the first two characters of each string.
[Sample String : 'abc', 'xyz' Expected Result : 'xyc abz']
5. Write a Python program to add 'ing' at the end of a given string (length should be at
least 3). If the given string already ends with 'ing' then add 'ly' instead. If the string
length of the given string is less than 3, leave it unchanged. Go to the editor
[Sample String : 'abc' Expected Result : 'abcing'
Sample String : 'string' Expected Result : 'stringly']

 Algorithm :
Write algorithm for the implementation of code for above mentioned program
Statements

Department of Electronics and Telecommunication Engineering [D.K.T.E’s TEI,Ich] 15


TY-BTECH-ETC ETP348 Python Programming Lab
---------------------------------------------------------------------------------------------------------------------------------------------------
Experiment No. – 07
CO Mapped : CO2 Level: L3
 Title: - Experiment on list manipulation in python
 Objectives:

 To understand the list manipulation operation


 Theory:
The list is a most versatile datatype available in Python which can be written as a list
of comma separated values items between square brackets. Important thing about a list is that
items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square
brackets.
For example −
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and
so on.
 Accessing Values in Lists:
To access values in lists, use the square brackets for slicing along with the index or
indices to
obtain value available at that index. For example −
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
When the above code is executed, it produces the following result −
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Updating Lists
You can update single or multiple elements of lists by giving the slice on the left-hand
side of the assignment operator, and you can add to elements in a list with the append method.
For example
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
result −
Value available at index 2 :
1997
New value available at index 2 :
2001

Department of Electronics and Telecommunication Engineering [D.K.T.E’s TEI,Ich] 16


TY-BTECH-ETC ETP348 Python Programming Lab
---------------------------------------------------------------------------------------------------------------------------------------------------
 Delete List Elements
To remove a list element, you can use either the del statement if you know exactly
which elements you are deleting or the remove method if you do not know.
For example −
list1 = ['physics', 'chemistry', 1997, 2000];
print list1
del list1[2];
print "After deleting value at index 2 : "
print list1
When the above code is executed, it produces following result −
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2:
['physics', 'chemistry', 2000]
 Program Statement :
 Write a Python program to sum all the items in a list.
 Write a Python program to get the smallest number from a list.
 Write a Python program to find the second largest number in a list.
 Write a Python program to create a list by concatenating a given list which range goes
from 1 to n.
Sample list : ['p', 'q']
n =5 Sample Output : ['p1', 'q1', 'p2', 'q2', 'p3', 'q3', 'p4', 'q4', 'p5', 'q5']
 Based on a list of fruits, create a new list, containing only the fruits with the letter "a"
in the name using list comprehension.
Sample list : fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

 Algorithm :
Write algorithm for the implementation of code for above mentioned program
Statements

Department of Electronics and Telecommunication Engineering [D.K.T.E’s TEI,Ich] 17

You might also like