Basics of Python Part 1
Basics of Python Part 1
BY IRFAN MALIK
LECTURE NO 6
STRINGS IN PYTHON
Strings in python are either surrounded by
single quotation mark or double quotation
marks. ‘hello’ is same as “hello”. You can
display string literal by print (). Use triple
quotation for multiline strings.
MODIFY STRINGS
Uppercase String
The upper () method returns the string into
upper case.
Lowercase String
The lower () method returns the string into
lower case.
Remove White Spaces
White space is the space before and/or
after the actual text, and very often you
want to remove this space. The strip ()
method removes any whitespaces from
the beginning or end of the text.
Replace String
The replace () string replaces the string
with another string.
Split String
The split () method returns a list where the
text between specified separator becomes
the list items. The split () method splits
the string into substring if it finds the
instances of the separator.
LIST
List is used to assign more than one
values to a variable. It is created using
square brackets [].
LIST ITEMS
List items are ordered, changeable, and
allow duplicate values. List items are
indexed, the first item has index[0], the
second item has index[1], etc. Allow
duplicates.
LIST LENGTH
To determine how many items a list have,
then use len() function.
TUPLE
Tuples are used to store multiple items in
a single variable. A tuple is a collection
which is ordered and unchangeable.
Tuples are written with round brackets.
TUPLE ITEMS
Tuple items are ordered, unchangeable
and allow duplicate values. Tuple items
are indexed, the first item is index(0), the
second item is index(1), etc. Allow
duplicates since tuples are indexed, they
can have items with same value.
CREATE TUPLE WITH ONE ITEM
To create tuple with one item, you have to
add comma after a string otherwise
Python will not recognize it.
PYTHON SETS
Sets are used to store multiple items in a
single variable. A set is a collection which
is unordered, unchangeable and
unindexed. Set items are unchangeable
but you can remove and add new items.
Sets are written with curly brackets {}.
Duplication not allowed Sets cannot
have different items with same value.
UNORDERED
Unordered means that items do not have
any specific order. Set items can appear in
different order every time you use them,
and cannot be referred to by index or key.
PYTHON DICTIONARIES
Dictionaries are used store data key:value
pairs. A dictionary is a collection which is
ordered, changeable and do not allow
duplicates.
DICTIONARY ITEMS
Dictionary items are ordered, changeable
and do not allow duplicates. Dictionary
items are presented in key:value pairs,
and can be referred to by using the key
name.