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

Python String and List Basics

The document outlines the syllabus for a Python course for engineers, focusing on string and list operations. It covers string manipulation techniques, including concatenation, slicing, and various string methods, as well as list creation, indexing, slicing, and built-in list methods. Each section provides syntax and examples to illustrate the concepts effectively.

Uploaded by

Soumya Panda
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 views64 pages

Python String and List Basics

The document outlines the syllabus for a Python course for engineers, focusing on string and list operations. It covers string manipulation techniques, including concatenation, slicing, and various string methods, as well as list creation, indexing, slicing, and built-in list methods. Each section provides syntax and examples to illustrate the concepts effectively.

Uploaded by

Soumya Panda
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

PYTHON FOR ENGINEERS

OEEC6205
4th Semester, B. Tech(CSE)
IGIT, Sarang
SYLLABUS

Module II
Strings, Creating and Storing Strings, Basic String
Operations, Accessing Characters in String by Index
Number, String Slicing and Joining, String Methods,
Formatting Strings.

Lists, Creating Lists, Basic List Operations, Indexing and


Slicing in Lists, Built-In Functions used on Lists, List
Methods, The del Statement.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


2
CSEA, IGIT, Sarang
STRING

 A string is a group of characters enclosed in between


single quotes (' ') or double quotes ("" "").
 A Multiline string is always enclosed in between triple
quotes (""" """).
 The data type is str.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


3
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


4
CSEA, IGIT, Sarang
STRING
MANIPULATION
OPERATORS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


5
CSEA, IGIT, Sarang
CONCATENATION OPERATOR ( + )

 It is used to concatenate two strings to make as one


string.
 Syntax:
variable = String-1 + String-2 + ...........
Where String-1, String-2 ...... can be a literal / variable.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


6
CSEA, IGIT, Sarang
ASTERISK OPERATOR ( * )
 It is used to repetition of a string required number of
times.
 Syntax:
variable_name = String * number_of_times
Where, the String can be a literal / variable.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


7
CSEA, IGIT, Sarang
SUB-SCRIPT OPERATOR ( [ ] )
 A string is a fixed sequence of characters.
 The first character appears at the 0th index, 2nd character at
1st index, etc.
 The [ ] and index value is used to retrieve a particular
character from the string.
 The [ ] and pair of index values are used to retrieve a subset
of characters from the string.
 SYNTAX FOR PARTICULAR CHARACTER:
variable = String[index]
The character from (index-1) of the string.
Where, the String can be a literal/ variable and the index
value starts from 0 onwards.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
8
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


9
CSEA, IGIT, Sarang
SYNTAX FOR SUBSET OF CHARACTERS
 The subset of characters from Bindex to (Eindex-1) of the
string
variable = String[Bindex:Eindex]
 The subset of characters from Bindex to the end of the
string
variable = String[Bindex:]
 The subset of characters from the starting to the (Eindex-1)
of the string
variable = String[:Eindex]
Where, the String can be a literal/ variable and the index
value starts from 0 onwards.
 When both the indices are used then the Bindex must be
less than the Eindex.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
10
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


11
CSEA, IGIT, Sarang
len ( ) FUNCTION

 It returns the length of a string.


 Syntax:
variable = len(Literal / Variable)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


12
CSEA, IGIT, Sarang
split( ) METHOD
 It splits a string up to multiple separate strings based on a specific
separator character such as a space or a comma.
 Syntax:
variable = [Link]('separator_character')
Where, the String can be a literal / variable and the
separator_character can be a space (' ') or a comma ( , ).

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


13
CSEA, IGIT, Sarang
count( ) METHOD
It counts the total numbers of substrings in a string.
Syntax:
Variable = Literal/[Link]('sub_string'/sub_string_variable)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


14
CSEA, IGIT, Sarang
replace( ) METHOD
 It replaces a substring with a new substring in a string.
 Syntax:
Variable = Literal/[Link](actual_string, replace_string)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


15
CSEA, IGIT, Sarang
find( ) METHOD
 It finds a substring from a given string.
 If the substring is present in the given string then it returns the
starting index of the substring; otherwise it returns -1 value.
 Syntax:
Variable = Literal/[Link]('sub_string'/sub_string_variable)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


16
CSEA, IGIT, Sarang
upper( ) METHOD

 It changes a string to uppercase.


 Syntax:
Variable = Literal/[Link]( )

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


17
CSEA, IGIT, Sarang
lower( ) METHOD

 It changes a string to lowercase.


 Syntax:
Variable = Literal/[Link]( )

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


18
CSEA, IGIT, Sarang
title( ) METHOD

 It changes the first character of every substring


into uppercase.
 Syntax:
Variable = Literal/[Link]( )

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


19
CSEA, IGIT, Sarang
swapcase( ) METHOD

 It changes the lowercase character to uppercase


and vice-versa in a string.
 Syntax:
Variable = Literal/[Link]( )

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


20
CSEA, IGIT, Sarang
format( ) METHOD
 It is used for formatting string.

 The formatted string is used for printing information


out or retrieving information from a program.

 SYNTAX (Replacing placeholder {} with


Literal/Variable):
Variable = '{} {}.....'.format(argument-1, argument-2,.......)

 The 1st placeholder {} replaces with argument-1, the


2nd placeholder {} replaces with argument-2 and so
on.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


21
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


22
CSEA, IGIT, Sarang
format( ) METHOD

 SYNTAX (Replacing placeholder {argument_number}


with Literal/Variable):
Variable = '{argument_1} {argument_2}.....'.format(argument-1,
argument-2,.......)

 The placeholder {argument_number} replaces with the


argument according to the position value of the
argument/parameter.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


23
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


24
CSEA, IGIT, Sarang
format( ) METHOD
 SYNTAX (Replacing placeholder {Variable} with
Literal/Variable):
Variable='{Variable1}{Variable2}.......'.format(Variable1=Value,
Variable2=Value,.....)

 Here, the arguments are Variable1, Variable2, .... etc.

 The placeholder {Variable} replaces with the


Variable-1, Variable-2,.... arguments of the format( )
method.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


25
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


26
CSEA, IGIT, Sarang
LIST
 A List is a general data structure widely used in Python
programs.

 It is referred to as dynamic arrays in other languages.

 It is both mutable and a sequence data type that allows


them to be indexed and sliced.

 A list can contain different types of objects, including other


list objects.
 Lists are created using square brackets ([ ]).
 Empty list can also be created using built-in function list( ).
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
27
CSEA, IGIT, Sarang
LIST
 SYNTAX:
1) EMPTY LIST CREATION:
List_Name = [ ]

2) LIST CREATION WITH ELEMENTS:


List_Name = [Element1, Element2, .......]

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


28
CSEA, IGIT, Sarang
LIST
 SYNTAX of list( ) FUNCTION:
1) EMPTY LIST CREATION
List_Name = list( )
2) LIST CREATION WITH ELEMENTS:
List_Name = list([Element1, Element2, .......] / Another_List)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


29
CSEA, IGIT, Sarang
LIST INDEXING
 Python lists are zero-indexed, and act like arrays in other
languages.
 Negative indices are used for counting from the end of
the list.
 The last element index is -1, and the next element index
would be -2, and so on.
 Attempting to access an index outside the bounds of the
list will raise an IndexError.
 Syntax:
list_name[index]

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


30
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


31
CSEA, IGIT, Sarang
LIST SLICING

 Lists allow to use slice notation as:


new_list_name = list_name[start:end:step]

 The output of the slice notation is a new list


containing elements from start index to (end-1)
index.
 If options are omitted, then start defaults to
beginning of the list, end to end of the list and step
to 1.
 list_name[start:end] – It returns a sub-list from
start index to end-1 index of the list.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
32
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


33
CSEA, IGIT, Sarang
LIST SLICING
 list_name[start:] – It returns a sub-list from start index to
end of the list.
 list_name[:end] – It returns a sub-list from 0th index to
end-1 index of the list.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


34
CSEA, IGIT, Sarang
LIST SLICING

 list_name[::-1] – It returns the reverse list of the whole


list.
 list_name[start:end:-1] – It returns the reverse list
from start index to end-1 index of the list.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


35
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


36
CSEA, IGIT, Sarang
LIST
METHODS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


37
CSEA, IGIT, Sarang
append ( ) Method
 It appends a new element or list at the end of the list.
 Syntax:
list_name.append(element / another_list)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


38
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


39
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


40
CSEA, IGIT, Sarang
extend ( ) Method
 It extends the list by appending elements from another
list.
 Syntax:
list_name.extend(another_list)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


41
CSEA, IGIT, Sarang
CONCATENATION OF LISTS USING +
 + operator concatenates more than one lists
to make as one list.
 Syntax:
List_Name = List1 + List2 + .........

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


42
CSEA, IGIT, Sarang
index ( ) Method
 It gets the index of the first occurrence of the element.

 Syntax:
list_name.index(element, [index_position])

 The searching of element starts by default from 0th


index of the list.

 If the index_position is provided, then the searching of


element starts from the index_position of the list.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


43
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


44
CSEA, IGIT, Sarang
insert ( ) Method

 It inserts an element at the specified index.


 Syntax:
list_name.insert(index, element)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


45
CSEA, IGIT, Sarang
pop ( ) Method
 pop( ): It removes and returns the last element from the
list.
 Syntax:
variable_name = list_name.pop( )

 pop(index): It removes and returns the specified index


element from the list.
 Syntax:
variable_name = list_name.pop(index)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


46
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


47
CSEA, IGIT, Sarang
remove ( ) Method
 It removes the first occurrence of the specified element
from the list.
 If the provided element is not found, then a ValueError
is raised.
 Syntax:
list_name.remove(element)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


48
CSEA, IGIT, Sarang
reverse ( ) Method
 It reverses the list in-place and returns None.

 Syntax:
list_name.reverse( )

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


49
CSEA, IGIT, Sarang
count ( ) Method
 It counts and returns the number of occurrences of the
given element in the list.

 Syntax:
Variable = list_name.count(element)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


50
CSEA, IGIT, Sarang
sort ( ) Method
 sort( ): It sorts the list in ascending order and returns
None.

 Syntax:
list_name.sort( )

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


51
CSEA, IGIT, Sarang
sort (reverse=True) Method
 sort(reverse=True): It sorts the list in descending order
and returns None.

 Syntax:
list_name.sort(reverse=True)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


52
CSEA, IGIT, Sarang
clear ( ) Method

 It removes all the elements from the list.


 Syntax:
list_name.clear( )

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


53
CSEA, IGIT, Sarang
del Keyword
 del list_name[index]: It deletes the specified index element.
 Example:

 del list_name[-1]: It deletes the last element in the list.


 Example:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


54
CSEA, IGIT, Sarang
del Keyword
 del list_name[::2]: It deletes all the even index positioned
elements.
 Example:

 del list_name[ : ]: It deletes all the elements in the list.


 Example:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


55
CSEA, IGIT, Sarang
LIST COPYING USING LIST SLICING
 Syntax:
new_list = old_list[:]
 It creates a new copy of the old_list list object and
refers by the new_list.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


56
CSEA, IGIT, Sarang
LIST COPYING USING list( ) FUNCTION
 Syntax:
new_list = list(old_list)
 It creates a new copy of the old_list list object and refers
by the new_list.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


57
CSEA, IGIT, Sarang
BUILT-IN
FUNCTIONS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


58
CSEA, IGIT, Sarang
len( ) FUNCTION

 It returns the length of a list.


 Syntax:
variable = len(list_name / [ele1, ele2, ......])

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


59
CSEA, IGIT, Sarang
max( ) and min( ) FUNCTION
 max( ) returns the maximum value from the list.
 Syntax:
variable = max(list_name / [ele1, ele2, ......])

 min( ) returns the minimum value from the list.


 Syntax:
variable = min(list_name / [ele1, ele2, ......])

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


60
CSEA, IGIT, Sarang
reversed( ) FUNCTION
 It reverses a list and returns the reversed object.
 Syntax:
variable = list(reversed(list_name / [ele1, ele2, ......]))

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


61
CSEA, IGIT, Sarang
sum( ) FUNCTION
 It returns the sum of the elements of the list.
 Syntax:
variable = sum(list_name / [ele1, ele2, ......])

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


62
CSEA, IGIT, Sarang
all( ) FUNCTION
 It returns true if all the elements are true.
 if the list is empty then it returns true.
 Syntax:
variable = all(list_name / [ele1, ele2, ......])

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


63
CSEA, IGIT, Sarang
any( ) FUNCTION
 It returns true if any element of the list is true.
 If the list is empty then it returns false.
 Syntax:
variable = any(list_name / [ele1, ele2, ......])

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


64
CSEA, IGIT, Sarang

You might also like