Download Intro. to Computing

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Intro. to Computing
Name:___________________
1. Strings in Python are sequential collections of only characters. A character is a string of length 1. In Python, the
for loop iterates once for each item in some sequence type (i.e, a list, tuple, string). What do you guess would be
printed by the following program if the user enters “Mark”?
name = raw_input(“Enter your name: “)
for ch in name:
print ch
print 'done'
2. Strings are immutable, but the sequence operations work on strings.
Operations on strings (or any sequence collection) include:
Operation
Operator
Explanation
Example
myString = “Hello World”
otherString = “cat”
Access the element specified myString[1]
Indexing
[ <index> ]
by the index
myString[ 1:5 ]
Slicing
[ : ]
Extract a part of the string
myString + otherString
Concatenation
+
Combine strings together
Concatenate a repeated
otherString * 3
Repetition
*
number of times
Ask whether a substring is in ‘orl’ in myString
Membership
in
a string
How many items are in the
len( myString )
Length
len(string)
string?
Result of Example
‘e’
‘ello’
‘Hello Worldcat’
‘catcatcat’
True
11
For the following strings, predict the results:
cheer = ‘GO Panthers!!!’
rhyme = ‘The cow jumped over the moon.’
01234567891111111111222222222
0123456789012345678
Expression
cheer[4]
Predicted Result
Actual Result
cheer[2:6]
cheer[:4]
cheer[1:4] + rhyme[-3:]
cheer[:2] * 3
‘jump’ in rhyme
len(cheer)
cheer[2:4]*4
Lecture 10 Page 1
Intro. to Computing
Name:___________________
3. String objects also have the following methods: (string module can be imported to provide more operations.)
Method
Usage
Explanation
center
myString.center(w)
Returns a string with myString centered in a field of size w
ljust
myString.ljust(w)
Returns a string with myString left-justified in a field of size w
rjust
myString.rjust(w)
Returns a string with myString right-justified in a field of size w
upper
myString.upper( )
Returns a string with myString in all upper-case characters
lower
myString.lower( )
Returns a string with myString in all lower-case characters
strip
myString.strip( )
Returns a string with leading and trailing whitespace (space, tab,
new-line) chars. removed. An optional string parameter can be used to
supply characters to strip instead of whitespace.
count
myString.count(sub)
Returns number of occurrences of sub in myString
(Optional parameters: myString.count(sub [, start [, end] ] )
endswith
myString.endswith(sub)
Returns True if myString ends with the substring sub; otherwise it
returns False
startswith myString.startswith(sub)
Returns True if myString starts with the substring sub; otherwise it
returns False
isdigit
myString.isdigit( )
Returns True if myString contains only digits; otherwise it returns False
isalpha
myString.isalpha( )
Returns True if myString contains only letters; otherwise it returns False
split
myString.split( )
Returns a list of substrings of myString splits at whitespace characters.
An optional string parameter can supply characters to split on.
find
myString.find(sub)
Returns the starting index of the first occurrence of sub.
(Optional parameters: myString.find(sub [, start [, end] ] )
replace
myString.replace(old,new) Returns a string with all occurrences of substring old replaced by
substring new. An additional integer parameter can specify the number
of replacements to perform, e.g.,myString.replace(old,new, 3)
Assume the following strings:
path = “/home/usr/fienup/”
str = ‘mouse’
data = ‘Python rules!’
What would be the result of each of the following?
a) path.find(‘om’)
b) path.count(‘/’)
c) path.split(‘/’)
d) path.upper().rjust(25)
e) data.endswith(‘i’)
f) “totally “.join(data.split())
4. Using the variable from the above question, what string methods would perform the following tasks:
a) Obtain a list of words in the string.
b) Obtain the string converted to all upper-case letters.
c) Locate the position of the substring “rules”.
d) Replace the exclamation point with a question mark.
Lecture 10 Page 2