Professional Documents
Culture Documents
Lec 03 PDF
Lec 03 PDF
Lec 03 PDF
Concept
K-03
2 / 20
Topic
●
String Manipulation :
1. a. subscript operator
2. b. iteration (loop) & string
3. c. function ord(), chr()
4. d. string methods
3 / 20
String
●
string can be perceived as sequence of characters
●
string can be compared using operator ==, > , <
●
string can be argument of certain function, ex. : len() function,
used to calculate the length of a string
●
string can also be an argument of a function, ex.
len( ) function used to calculate the length of a string
s_str = "abcde"
len(s_str) # what is the output ... ?
4 / 20
string - subscript operator [ ]
●
square brackets are used to access value of a string at certain
position
s = "abc"
index from the front : 0, 1, 2
s[0] # 'a'
s[1] # 'b'
s[2] # 'c'
s[3] # error, out of bounds
5 / 20
string- subscript operator [ ]
●
square brackets are used to access value of a string at certain
position
s = "abc"
index from behind : -3, -2, -1
s[-1] # 'c'
s[-2] # 'b'
s[-3] # 'a'
s[-4] # error, out of bounds
6 / 20
string - slicing
●
string can be sliced using [start : stop : step]
●
the default value of "step" is 1; not needed to be specified
●
"start" and "stop" can also be not specified
s = 'abcdefgh'
s[3:6] # "def"
s[3:6:2] # "df"
s[ : : ] # "abcdefgh"
s[ : : -1 ] # "hgfedbca"
s[4 : 1 : -2 ] # "ec"
7 / 20
string - immutability
●
string is "immutable" - means its content can not be modified /
changed
s = "hello"
s[0] = 'y' # error
8 / 20
substring
●
to check if a substring is in a string, use operator "in" :
example :
s = "Bandung Jawa Barat"
brt = "Barat" in s # boolean
print("Ada kata Barat:", brt )
9 / 20
review : loop "for"
●
"for" loop is used to make a certain number of iteration
●
the arguments of "range" can also be a certain value, in
addition to numbers
10 / 20
String and Loop
●
iteration using string as the argument of "range"
●
# method 1
s = "bandung"
for index in range ( len(s) ) :
if s[index] == 'd' :
print ( "ada huruf d")
●
# method 2
for char in s :
if char == 'd' :
print ( "ada huruf d" )
11 / 20
String and Loop
●
what if we use "while" ?
s = "bandung"
...
while
...
●
clue : use a "counter"
●
compare with method #1 or method #2
12 / 20
String and Loop
●
s = "bandung"
●
question : what if the output we want is :
"ada huruf u pada karakter ke: 5"
●
using method #1 ?
●
using method #2 ?
13 / 20
Exercise : string and loop
●
exercise : create a program to find letters which are exist in
two strings :
s1 = "bandung"
s2 = "jawa barat"
h_sama = " "
...
14 / 20
Function ord() dan chr()
●
each character has an ASCII code
●
function ord(c) will return ASCII code (ordinal position) of
character c
example : ord('a') # returns 97
●
function chr(x) is the opposite of function ord(), will return the
character of ASCII code x
example : chr(97) # returns character 'a'
15 / 20
string methods
●
string object has "method", which can be accessed by using
"dot" notation
ex. : s = "Bandung Jawa Barat"
●
split() : returns a list of words from a string, ex. :
s.split() # [ 'Bandung' , 'Jawa' , 'Barat' ]
s.split('a') # ['B', 'ndung J', 'w', ' B', 'r', 't']
●
find(sub) : returns the lowest index of s, if substring "sub" is
found, ex. :
s.find("Ja") #8
16 / 20
string methods
●
lower() : returns a string with lowercase letter, ex. :
s.lower() # 'bandung jawa barat'
●
upper() : returns a string with uppercase letter, ex. :
s.upper() # 'BANDUNG JAWA BARAT'
●
replace(old, new) : returns a string in which the "old" substring
is replaced with the "new" substring, ex. :
s.replace("Barat", "Timur") # 'Bandung Jawa Timur'
17 / 20
string methods
●
strip(sub) : returns a string in which the first and the last
characters are truncated if the same as substring "sub", ex.:
s.strip("Bat") # 'ndung Jawa Bar'
●
startswith(sub) : returns True if s is started with "sub", ex. :
s.startswith("Ban") # True
●
endswith(sub) : returns True if s is ended with "sub", ex. :
s.endswith("rat") # True
18 / 20
Exercise
●
variable story berisi text
●
hitung jumlah kata "kucing" dalam text tersebut
19 / 20
Summary
●
subscript operator [ ]
●
immutable string
●
string and loop
●
function len(), ord() & chr()
●
string methods : split, find, lower, upper, replace, strip,
startswith, endswith
20 / 20