Professional Documents
Culture Documents
2
Strings
• String: A sequence of text characters in a program
– Strings start and end with quotation mark " or apostrophe ' characters.
– Examples:
"hello"
"This is a string"
"This, too, is a string. It can be very long!"
• A string may not span across multiple lines or contain a " character:
"This is not
a legal String."
"This is not a "legal" String either."
• A string can represent characters by preceding them with a
backslash
– \t tab character
– \n new line character
– \" quotation mark character
– \\ backslash character
3
Indexes
– Example:
print name, "starts with", name[0]
Output:
Hello Bob starts with H 4
Indexes
5
Slicing
• Indexing returns a string containing a single character from a larger
string
• We can also access a contiguous sequence of characters, called a
substring, through a process called slicing
• Slicing:
– <string>[<start>:<end>]
– start and end should both be integer
• The slice contains the substring beginning at position start and runs
up to but doesn’t include the position end. Example:
>>> name[0:3]
'Hel'
>>> name[5:9]
' Bob'
>>> name[:5]
'Hello'
>>> name5:]
' Bob'
>>> name[:]
'Hello Bob'
6
String Operations
• Can we put two strings together into a longer string?
• Concatenation “glues” two strings together (+)
• Repetition builds up a string by multiple concatenations
of a string with itself (*)
• Example:
>>> "Spam" + "And" + "Eggs"
'SpamAndEggs'
>>> 3 * "spam"
'spamspamspam'
7
String Operations
Operator Meaning
+ Concatenation
* Repetition
<string>[] Indexing
<string>[:] Slicing
len(<string>) Length
For <var> in <string> Iteration through characters
8
Strings
Output:
What's your name? Tiger Woods
Tiger Woods... what a silly name!
9
Text Processing
• Text processing: Examining, editing, formatting text.
– often uses loops that examine the characters of a string one by one
10
Strings and Numbers
11
String Functions
• lower() is a function that converts a string into lowercase. Example:
message = "Hello"
print message
message.lower() #no effect print message
message = message.lower()
print message
Output:
Hello
Hello
hello
12
String Functions
13
String Functions
14
String Functions
15
Lists
16
Lists and Arrays
• A list or array is a sequence of items where the entire sequence is
referred to by a single name (i.e. s) and individual items can be
selected by indexing (i.e. s[i]).
• Python List:
– Python lists are dynamic. They can grow and shrink on demand.
– Python lists are also heterogeneous, a single list can hold arbitrary data types.
– Python lists are mutable sequences of arbitrary objects.
17
Basic List Operations
Operator Meaning
<seq> + <seq> Concatenation
<seq> * <int-expr> Repetition
<seq>[] Indexing
len(<seq>) Length
<seq>[:] Slicing
for <var> in <seq>: Iteration
<expr> in <seq> Membership (Boolean)
18
Example
19
Example
Example 1:
some_list = [3,6,2,5]
i=1
while i < 3:
print some_list[i], ";"
i=i+1
print some_list[3]
Example 2:
NAMES = ["Alice", "Bob", "Cathy", "Doug"]
for i in range(len(NAMES )):
print "Hello", NAMES [i]
i=i+1
print "We're out of names!"
print "The last name we saw was", NAMES[i]
20
List Member Functions
Method Meaning
<list>.pop(i) Deletes the ith element of the list and returns its value.
22
Example
23
Call By Reference
example_list = []
def append_one(my_list):
#"This is a function that adds something to the end of a
list"
Heap
my_list.append(1)
Name,type value
print id(my_list) [1]
example_list, list
print id(example_list) my_list, list (local)
append_one(example_list)
print example_list
24
Nested Lists
25