0% found this document useful (0 votes)
40 views35 pages

Lecture 2

The document discusses various operations that can be performed on strings and lists in Python. It explains that strings can be treated like arrays to access individual characters. Lists allow storing heterogeneous data and support common operations like accessing, adding, removing and iterating over elements. Some key string operations covered include finding length, checking for substrings, slicing and modifying case. Common list operations discussed are creation, accessing/slicing elements, sorting, joining and comparing lists.

Uploaded by

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

Lecture 2

The document discusses various operations that can be performed on strings and lists in Python. It explains that strings can be treated like arrays to access individual characters. Lists allow storing heterogeneous data and support common operations like accessing, adding, removing and iterating over elements. Some key string operations covered include finding length, checking for substrings, slicing and modifying case. Common list operations discussed are creation, accessing/slicing elements, sorting, joining and comparing lists.

Uploaded by

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

Strings in Python

28/10/2023 Dr. Sannidhan M Shetty 1


String Operations
1. String as array
Any variable when assigned with string data, it automatically works like
an array to access the characters sequentially

For example:

28/10/2023 Dr. Sannidhan M Shetty 2


String Operations
2. Iterating the String data
• String data can be automatically iterated to carry out certain operations using for
loop.
• Each and every character of the string is sequentially accessed and automatically
assigned to the control variable of the for loop.

Example:

28/10/2023 Dr. Sannidhan M Shetty 3


String Operations
3. Finding length of the string
• len() method is used to compute the length of any string.

28/10/2023 Dr. Sannidhan M Shetty 4


String Operations
4. Checking if a string is a substring of another string
• We can check whether a particular string/character is a substring of another string
or not using a membership operator in or not in

Example:

28/10/2023 Dr. Sannidhan M Shetty 5


String Operations
5. Slicing the string
• Slicing is used to extract the substring from a string variable by specifying the
starting index and ending index which represents the starting and ending position
of characters respectively

28/10/2023 Dr. Sannidhan M Shetty 6


String Operations
5. Slicing the string

Example:

28/10/2023 Dr. Sannidhan M Shetty 7


String Operations
5. Slicing the string
Different variations of slicing:
1. Slicing from the start: variable[:end]
2. Slicing all the way till the end: variable[start:]
3. Slicing from the right side: String can be extracted from the right side through
negative indexing

28/10/2023 Dr. Sannidhan M Shetty 8


String Operations
String extended operations:
1. String is immutable: String object does not support re assignment

2. Converting the string to upper case: a method upper() is used which converts all
the characters to uppercase

3. Converting the string to lower case: a method lower() is used which converts all the
characters to lowercase

28/10/2023 Dr. Sannidhan M Shetty 9


String Operations
String extended operations:

4. Checking if the string is upper case/lower case:

5. To reverse a string :

28/10/2023 Dr. Sannidhan M Shetty 10


String Operations
String extended operations:

6. capitalize() : The method capitalizes the first letter of the string

Syntax: string.capitalize()

7. title(): converts the string to the title format by capitalizing first letter of every word

Syntax: string.title()

8. count(): counts the total occurrences of substring in a given string

28/10/2023 Dr. Sannidhan M Shetty 11


String Operations
String extended operations:

9. find(): returns the index value of first occurrence of a substring with in specified
begin and end index positions.

10. * operator : * operator with string is used to repeat the occurrences of a string.

28/10/2023 Dr. Sannidhan M Shetty 12


Functions in Python

28/10/2023 Dr. Sannidhan M Shetty 13


Functions in Python
• Python supports function (module) oriented programming, where a sequence of task
can be divided into number of modules for the accomplishment of required task.

28/10/2023 Dr. Sannidhan M Shetty 14


Function returning a value
• Using the keyword return inside the function will return the value to the function call

1. Function returning a single value: A function on performing the required task


returns a single value to the calling function.

2. Function returning multiple values: A function on performing the required task


can also return multiple values using a single return statement. Each value in this case
is separated by comma.

28/10/2023 Dr. Sannidhan M Shetty 15


Function returning a value

28/10/2023 Dr. Sannidhan M Shetty 16


Function with default arguments
• A function parameter can be assigned with a default value to call the function with
a smaller number of arguments than the number of parameters.

28/10/2023 Dr. Sannidhan M Shetty 17


Python Lists

28/10/2023 Dr. Sannidhan M Shetty 18


Python lists
• Lists are used to sequentially store the collection of data items of
similar/dissimilar types
• list belongs to the class list
• Like sequential arrays, list is an indexed data structure, and the data items are
stored under the different index values in the ordered way as according to its
creation. i.e., the first data item is stored under index [0] and so on.

28/10/2023 Dr. Sannidhan M Shetty 19


Basic list operations
1. Using constructor to create list

2. Splitting characters of the string into list


• A list when constructed using string argument automatically splits the individual
characters of the string and stores them sequentially one after the other in the list.

28/10/2023 Dr. Sannidhan M Shetty 20


Basic list operations
3. Splitting string into list based on the separator
• A string argument can be split into different elements of the list sequentially using
split() method.

• sep is a separator that is optional argument. Default separator is single space.

4. Finding the length of the list

28/10/2023 Dr. Sannidhan M Shetty 21


Access Operation
• Any data item in the list can be accessed by specifying the valid index value of the list

when the index value is positive list is accessed from the left side

when the index is negative list is accessed from the right side

28/10/2023 Dr. Sannidhan M Shetty 22


Access Operation
Accessing the range of values from the list (Slicing the list)

A range of data items can be accessed starting from a particular index of a lower value
and ending with an index of higher value.

28/10/2023 Dr. Sannidhan M Shetty 23


Adding items to the list
Under this, we discuss different methods that is used to insert the data items into the
existing list.

1. append(): This method inserts the data item to the end of the list.

2. insert(): This method inserts a data item into the specific index value pushing the
existing data items to the right

28/10/2023 Dr. Sannidhan M Shetty 24


Adding items to the list
3. extend(): The extend method appends the elements of one list into another list

28/10/2023 Dr. Sannidhan M Shetty 25


Removing items from the list
1. remove(): This method is used to remove a specified data item from the list

2. pop(): This method removes(pops) a data item at specified index from the list

28/10/2023 Dr. Sannidhan M Shetty 26


Removing items from the list
3. del: This is an operator used to delete the data item from a specific index of the list.

4. clear(): This method removes all the data items from the list making it empty.

28/10/2023 Dr. Sannidhan M Shetty 27


Iterating through the list
• A for loop can automatically iterate through the data items of the list sequentially.

28/10/2023 Dr. Sannidhan M Shetty 28


Sorting operations in list
1. sort(): The sort() method sorts a given items or numerical data items in ascending
order by default uniform datatype list containing the string data

28/10/2023 Dr. Sannidhan M Shetty 29


Finding maximum and minimum element
1. To find the maximum element: Maximum element from the list can be retrieved
by using the method max as according to the following syntax

2. To find the minimum element: Minimum element from the list can be retrieved by
using the method min as according to the following syntax

28/10/2023 Dr. Sannidhan M Shetty 30


Retrieving the index of the list
• The index() method returns lowest index value where a particular element is found.

28/10/2023 Dr. Sannidhan M Shetty 31


Joining the lists
• Two lists can be joined (concatenated) using following methods:

28/10/2023 Dr. Sannidhan M Shetty 32


Comparing two lists
• Two lists can be compared by placing a comparison ‘==’ operator in between two
lists. If they are equal, it. returns true else returns false.

28/10/2023 Dr. Sannidhan M Shetty 33


Lab Questions
1. Write a program to count the number of each vowel in a given string.

2. Write a program to count the number of capital letters and display the position of
each capital letter in a user entered string via keyboard

3. Consider two strings, String1 and String2 and display the merged string as output.
The merged string should be the capital letters from both the strings in the order they
appear.
Sample Input:String1:ILikeC String2:MaryLikesPython
Merged string should be ILCMLP
4. Write a program to remove all punctuations like “’!()[]{};:’’’,\,<>,/,?,@,#,$,
%^&*_~” from the string provided by the user.

5. Write a binary search function which searches an item in a sorted list. The function
should return the index of element to be searched in the list.
28/10/2023 Dr. Sannidhan M Shetty 34
Lab Questions
6. Write a python program to create two separate lists of integers. List elements of both
the list shall be read from keyboard input. Program should display “Lists are
symmetrical” if both the lists contain equal number of even and odd numbers.

7. Write a function that returns the index of the smallest element in a list of integers. If
the number of such elements is greater than 1,return the smallest index. Use the
following header: def indexOfSmallestElement(lst): Write a program that prompts
the user to enter a list of numbers, invokes this function to return the index of the
smallest element and displays the index

28/10/2023 Dr. Sannidhan M Shetty 35

You might also like