Python Basics II, Strings
Introduction to Computers and Programming
Week 10
Stanley Chang
11/03/2025
1
Condition
x = 9
Output: Smaller than 10
if x < 10 :
print(‘Smaller than 10')
else:
print('Not Smaller than 10')
2
Condition
x = 9
Output: Smaller than 10
if not x < 10 :
print('Not Smaller than 10')
else:
print(‘Smaller than 10')
3
Condition
x = 9
if not x ==9 has the sane effect as if x!=9
if not x ==9 :
print('Not equal to 10')
else:
print('Equal to 10')
4
Condition
x = 14
if x < 10 :
print('Smaller than 10') Output: Smaller than 15
elif x < 15:
print('Smaller than 15’)
else:
print('Larger than or equal to 15')
5
Condition: Inline If
day = 6
is_weekend = True if day==6 or day==7 else False
Do TASK 1 if CONDITION is True else do TASK 2
print (is_weekend)
day = 6
print ("weekend") if day==6 or day==7 else print ("weekday")
6
Loops
x=1 x=1
while x<6: while True:
print ('weekday’) print ('weekday’)
x+=1 x+=1
if x>5:
break
7
Loops
for x in [0,1,2]:
print (x)
Iterate over the
members of a
letters = ['A', 'B', 'C'] sequence
for letter in letters :
print('Letter:', letter)
print('Done!')
8
Loops
for x in range(0, 3):
print (x) range() in Python 3.X is
equivalent to xrange()in
Python 2.X . There is no
xrange()in Python 3.x
for x in xrange(0, 3):
print (x)
9
Loops
for x in range(10): 0, 1,2,3,4,5,6,7,8,9,
print (x)
for x in range(2,10): 2,3,4,5,6,7,8,9,
print (x)
for x in range(2,10,3): 2,5,8
print (x)
10
Functions
• Much more built-in functions in Python you can use
than in C.
• We define a function using the def keyword
def generate_welcome_msg(name): define
return ”welcome ” + name
msg = generate_welcome_msg(”Stanley”) call
11
Functions
• Another example of calling a function
def checkIfPrime (number):
for i in range(2, number):
if (number%i == 0): define
return False
return True
number = 17
if checkIfPrime(number): call
print("Prime number")
12
Pass-by-Value or Pass-By-Reference?
• Neither
• First remember that variable assignment is to bind a name
to an object.
• Whether the object being passed to a function could be
modified in that function depends on its mutability
• Mutability decides whether you can change the content of the
object without changing its identity
• Immutable (unchangeable) objects
• bool, int, float, string, tuple, etc.
• Mutable (changeable) objects
• list, dict, set, etc.
13
Mutable vs. Immutable
def add_one(x): def add_one(lst):
x = x + 1 [Link](1)
print("Inside:", x) print("Inside:", lst)
a = 10 nums = [0]
add_one(a) add_one(nums)
print("Outside:", a) print("Outside:", nums)
Result: Result:
Inside: 11 Inside: [0, 1]
Outside: 10 Outside: [0, 1]
14
Strings
• Can be enclosed by either double or >>> s="hi"
single quotes >>> s
• "Hi" , 'Hi' 'hi'
>>> s + " Stanley"
• You can use + to concatenate two 'hi Stanley'
strings >>> len(s)
• 'Hi'+ ' Stanley' = 'Hi Stanley' 2
• We can use len() to return the length of >>> len(s+" Stanley")
a string 10
>>> int("100")
• We can use int() to convert a number in 100
a string to an integer
15
Strings >>> s = input("what's
your age?")
what's your age?30
• So, you can use conversion >>> age = int(s) + 1
functions to conveniently handle >>> age
users’ input. 31
>>> s = input("what's your
• It is important to use age?")
try/except to capture what's your age?18, as always
unexpected/bad format >>> try:
... age = int(s)
... except ValueError:
... print("bad age!")
...
bad age! 16
Getting Elements of a String
• We can use an index to get any single >>> s="Apple"
character of a string. >>> s[0]
'A'
A p p l e >>> s[5]
Traceback (most recent call
• You get an error if you attempt to index last):
beyond the end of a string
File "<stdin>", line 1,
• You can use a negative index! in <module>
• A negative index counts back from the end IndexError: string index
of the string out of range
• It gives easy access to the characters at
the end of the string. >>> s[-1]
• s[-1] is the last character 'e'
• Different from C! >>> s[-2]
'l' 17
>>> s="Apple"
Slicing String >>> s[1:3]
'pp'
>>> s[1:]
• We can also get continuous section 'pple'
of a string using : >>> s[:3]
• E.g. "apple"[1:3] = "pp" 'App'
• The first number is included. But the >>> s[:-1]
second number is one beyond the 'Appl'
end of the slice , thus not included >>> s[-1:]
• Leave off the first number or the last 'e'
number of the slice assumes the >>> s[:10]
beginning or end of the string 'Apple'
respectively (e.g. [:3], [2:]) >>> s[-10:]
• If the second number is out of 'Apple'
bound, it stops at the end >>> s[10:]
'' 18
Checking String Membership
>>> s="Apple"
• You can use in to check if a
>>> "A" in s
string is in another string True
>>> "a" in s
• The in operator returns True or False
False, which can be supplied to >>> "p" in s and "e" in s
an if statement. True
>>> if "p" in s and "e" in
s:
... print ("both p and e
are in Apple")
...
both p and e are in Apple
19
Comparing Strings
• s1 is less than s2 if either >>> s1="Apple"
one of the following >>> s2="Banana"
>>> s1 > s2
conditions is satisfied:
False
• The first i characters of s1 and >>> s1 == "Apple"
s2 match, but the (i+1)st True
character of s1 is less than the >>> s1 == "Applf"
(i+1)st character of s2. False
• All characters of s1 match s2, >>> s1 == "Applee"
but s1 is shorter than s2. False
20
Looping through Strings
>>> s = 'hello'
>>> index = 0
>>> while index < len(s):
... char = s[index]
... print(index, char)
We can loop through a string using ... index = index + 1
a while and the len() function ...
0 h
1 e
2 l
3 l
4 o
21
Looping through Strings
>>> s = 'hello'
>>> for char in s:
... print (char)
...
h
We can use a more elegant way: a e
for loop. l
l
o
22
Looping through Strings
>>> s = 'hello'
>>> for index, char in
enumerate(s):
... print (index, char)
...
You can use the enumerate ()
0 h
function to also obtain the index 1 e
along with the value for each item 2 l
3 l
4 o
23
enumerate()
[Link] 24
Iterator and Iterable >>> x=enumerate("Hello")
>>> y=iter("Hello")
>>> next(y)
'H'
• Iterable >>> next(x)
• an object that you can get an (0, 'H')
iterator from >>> next(y)
'e'
• E.g. list, dictionary, tuple, string >>> next(x)
• Iterator (1, 'e')
• An object representing a stream of >>> next(x)
data (2, 'l')
>>> next(x)
• Repeated calls to the (3, 'l')
iterator’s __next__() method (or >>> next(x)
passing it to the built-in (4, 'o')
function next()) return successive
items in the stream. When no more >>> next(x)
Traceback (most recent call last):
data are available
File "<stdin>", line 1, in <module>
a StopIteration exception is raised
instead. StopIteration
• iter(), enumerate() 25
>>> s = "Apple"
>>> [Link]()
String Methods 'apple'
>>> [Link]().upper()
• There are many frequently 'APPLE'
>>> s= " Apple "
used string methods/functions >>> [Link]()
in the string library 'Apple'
>>> s= " Apple "
[Link](), [Link]() -- returns the lowercase >>> [Link]()
or uppercase version of the string 'Apple '
[Link]() -- returns a string with whitespace >>> [Link]()
removed from the start and end ' Apple'
[Link]()/[Link]()/[Link]()... -- tests if >>> [Link]()
False
all the string chars are in the various >>> s= "Apple"
character classes >>> [Link]()
[Link]('other'), [Link]('other') -- True
tests if the string starts or ends with the >>> [Link]("A")
given other string True
26
[Link]
String Methods >>> [Link]("pple")
1
• [Link]('other') -- searches for the given >>> [Link]("e")
4
other string (not a regular expression) >>> [Link]("haha")
within s, and returns the first index where it -1
begins or -1 if not found >>> [Link]("e","a")
• [Link]('old', 'new') -- returns a string 'Appla'
where all occurrences of 'old' have been >>> [Link]("l")
['App', 'e']
replaced by 'new' >>> [Link]("p")
• [Link]('delim') -- returns a list of substrings ['A', '', 'le']
separated by the given delimiter. >>> [Link](['1','2'])
• [Link](list) -- opposite of split(), joins the '1Apple2'
elements in the given list together using the >>> s="--"
>>> [Link](['1','2'])
string as the delimiter. e.g. '---'.join(['aaa', '1--2'
'bbb', 'ccc']) -> aaa---bbb---ccc
27
[Link]
String Methods
• Since strings are immutable >>> s="Apple"
>>> id([Link]())
objects, if the methods return a
4305951016
string, they return a new string
>>> id([Link]())
that has been altered instead of 4305950736
modifying its own content. >>> id([Link]())
4305951128
>>> [Link]("A","B")
'Bpple'
>>> [Link]("A","C")
'Cpple'
>>> [Link]("X","C")
'Apple' 28
String Methods
• There are various ways to >>> s = "Bpple, bpple"
use these methods to >>> lower = [Link]()
>>> index =
process a string
[Link]("b")
• E.g. before using find() or
>>> index
replace() we first convert
the string to lower case so 0
we can look for a string >>> new =
regardless of case [Link]("b","a")
>>> new
'apple, apple'
29
String Methods
• Make use of the official documentation!
• [Link]
>>> s="Apple"
>>> type(s)
<class 'str'>
>>> dir(s)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper',
'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind',
'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] 30
Example: Parsing and Extracting
From armuro@[Link] Mon Nov 27 [Link] 2017
How do we extract these elements?
• Email sender
• Domain
• Date
• Time (hour, minute, second)
31
Getting Strings from Files
• A text file can be thought of as a sequence of lines
• We use open() to read a file, and then get the content
• open() returns a “file handle” used to perform operations on the file
• mode is optional. We put 'r' if we want to read the file. We put 'w' if we
want to write to the file
• [Link](size) reads some quantity of data and returns it as a string
• When size is omitted or negative, the entire contents of the file will be read and
returned.
• If the end of the file has been reached, [Link]() will return an empty string ("").
• [Link]() reads a single line from the file.
• [Link]() read all the lines of a file in a list
32
Opening a File
>>> fhandle = open("[Link]", "r")
>>> lines = [Link](10)
>>> lines Continue reading the file
'Congratula' instead of starting over
>>> lines = [Link]()
>>> lines
'tion, a donation of $700,000.00 has been made to you by Mr Azim Premji for more
information and claim reply me direct.\n'
>>> lines = [Link]()
>>> lines
['\n', '\n', 'Thanks\n', 'Mr Azim Premji']
>>> fhandle = open("[Link]", "r") Repoen the file to read again
>>> lines = [Link]()
>>> lines
['Congratulation, a donation of $700,000.00 has been made to you by Mr Azim
Premji for more information and claim reply me direct.\n', '\n', '\n',
'Thanks\n', 'Mr Azim Premji']
>>> 33
Opening a File
>>> fhandle = open("[Link]", "r")
>>> lines = [Link](10)
>>> lines
'Congratula'
>>> lines = [Link]()
>>> lines
'tion, a donation of $700,000.00 has been made to you by Mr Azim Premji for more
information and claim reply me direct.\n '
>>> lines = [Link]()
>>> lines
['\n', '\n', 'Thanks\n', 'Mr Azim Premji'] Notice that "\n" indicating the
>>> fhandle = open("[Link]", "r")
>>> lines = [Link]()
end of a line is captured.
>>> lines
['Congratulation, a donation of $700,000.00 has been made to you by Mr Azim
Premji for more information and claim reply me direct.\n', '\n', '\n',
'Thanks\n', 'Mr Azim Premji']
>>> 34
Opening a File
• When the file is missing…
>>> fhandle = open("[Link]", "r")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory:
'[Link]'
35
Opening a File
• The read() function returns a big string of the file. This is not ideal
when the content is large.
>>> fhandle = open("[Link]")
>>> content = [Link]()
>>> print(len(content))
153
>>> fhandle = open("[Link]")
>>> content = [Link]()
>>> print(len(content))
6687003
>>>
36
Opening a File
• While we can loop through a list of lines generated by readlines(), we
can directly loop through the file using the file handle
>>> fhandle = open("[Link]")
>>> count = 0
>>> for line in fhandle:
... count = count+1
... print (line)
...
Congratulation, a donation of $700,000.00 has been made to you by
Mr Azim Premji for more information and claim reply me direct.
>>> print (count)
5
37
>>>
So, you can use String functions in a File
• E.g. Searching lines that start with “From:”
>>> fhandle = open("[Link]")
>>> count = 0
>>> for line in fhandle:
... if [Link]("From:"):
... count+=1
...
>>> print (count)
1797
38
So, you can use String functions in a File
• Remember that read() will capture “newline” characters when
printing
• Sometimes you want to use the strip() function to remove them when
printing.
>>> fhandle = open("[Link]")
>>> fhandle = open("[Link]") >>> lines = [Link]()
>>> lines = [Link]() >>> for line in lines[:10]:
>>> for line in lines[:10]: ... line = [Link]()
... print (line) ... print(line)
... ...
From [Link]@[Link] Sat Jan 5 From [Link]@[Link]
[Link] 2008 Sat Jan 5 [Link] 2008
Return-Path:
<postmaster@[Link].o
Return-Path: rg>
<postmaster@[Link]>
39
Filtering the file content
• Make use of continue to skip lines you don’t need
>>> fhandle = open("[Link]")
>>> for line in fhandle:
... line = [Link]()
... if not [Link]("From:"):
... continue
... print(line)
...
From: [Link]@[Link]
From: louis@[Link]
From: zqian@[Link]
From: rjlowe@[Link]
From: zqian@[Link]
40
Filtering lines in the file
• You can apply a variety of criteria
>>> fhandle = open("[Link]")
>>> for line in fhandle:
... line = [Link]()
... if "[Link]" not in line:
... continue
... print(line)
...
Return-Path: <postmaster@[Link]>
for <source@[Link]>;
for <source@[Link]>; Sat, 5 Jan 2008 [Link]
+0000 (GMT)
for <source@[Link]>; Sat, 5 Jan 2008 [Link] -0500
for source@[Link]; Sat, 5 Jan 2008 [Link] -0500 41
Prompt for File Name
>>> fname = input("please enter a file name ")
please enter a file name [Link]
>>> fhandle = open(fname)
>>> for line in fhandle:
... print(line)
...
Congratulation, a donation of $700,000.00 has been made
to you by Mr Azim Premji for more information and
claim reply me direct.
>>>
42
Prompt for File Name
>>> fname = input("please enter a file name ")
please enter a file name [Link]
>>> try:
... fhandle = open(fname)
... except FileNotFoundError:
... print ("bad file name!")
...
bad file name!
>>>
43