You are on page 1of 29

CS 121

Object Oriented
Programming
Lecture 04
By: Syed Shahrooz Shamim
Programming with

PYTHON
Strings are Arrays Like many other popular
programming languages, strings in Python are

String
arrays of bytes representing unicode
characters.

However, Python does not have a character


String Slicing data type, a single character is simply
a string with a length of 1.

Square brackets can be used to access


elements of the string.
String
Indexing
String Slicing
String
Indexing
String Slicing
String Slicing
Activity
Entre a name and print it
in reverse order
Solution
Useful Function

Note:-
Len() is function And rest are method().
Method are always accessed with . DOT
Replace() method (String)

Note:- Replaces spaces in text with underscore


Replace() method (String)
find() Method in String
find() Method in String

Note:-
Count from h which is in position 0
and I of is at 3rd position.
find() Method in String

Note:- Here it starts counting from


5th position if is comes before it, It
wont be counted.
String are immutable
Note:-
Here ‘j’ of john
string have been
replaced with ‘y’
and it is printed but
it haven’t changed
original string it
have created a new
string while original
string remains
same un changed
(Immutable)
replace() with string
List
Finding elements
Lists are used to store multiple items in a single variable. 

Lists are one of 4 built-in data types in Python used to store


collections of data, the other 3 are Tuple, Set, and Dictionary, all
with different qualities and usage.

a list is created by placing all the items (elements) inside square


brackets [] , separated by commas.

It can have any number of items and they may be of different


types (integer, float, string etc.).

A list can also have another list as an item. This is called a


nested list.
Finding elements in List
Useful Function In List : Count()

Note:-
Used to count specific items in list
Useful Function In List : Sort()
Useful Function In List : Clear()

Note :- it clears all the contents of


the list
Useful Function In List : Copy()
Concatenating List
Useful Function In List : extend()
List Comparison
List Comparison
Using loops in list

You might also like