Summer10 Strings Nup
Summer10 Strings Nup
Introduction to Programming in Python A string is a sequence of characters. Python treats strings and
characters in the same way. Use either single or double quote
Strings
marks.
Texas Summer Discovery Slideset 10: 1 Strings Texas Summer Discovery Slideset 10: 2 Strings
... ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
32 ! ” # $ % & ’ ( ) * + , - . /
... ... 48 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
Texas Summer Discovery Slideset 10: 3 Strings Texas Summer Discovery Slideset 10: 4 Strings
Unicode Operating on Characters
Notice that:
ASCII codes are only 7 bits (some are extended to 8 bits). 7 bits The lowercase letters have consecutive ASCII values
only allows 128 characters. There are many more characters than (97...122); so do the uppercase letters (65...90).
that in the world.
The uppercase letters have lower ASCII values than the
uppercase letters, so “less” alphabetically.
Unicode is an extension to ASCII that uses multiple bytes for
character encodings. With Unicode you can have Chinese There is a difference of 32 between any lowercase letter and
characters, Hebrew characters, Greek characters, etc. the corresponding uppercase letter.
Texas Summer Discovery Slideset 10: 5 Strings Texas Summer Discovery Slideset 10: 6 Strings
Texas Summer Discovery Slideset 10: 7 Strings Texas Summer Discovery Slideset 10: 8 Strings
Creating Strings Functions on Strings
Function Description
Strings are immutable meaning that two instances of the same len(s) return length of the string
min(s) return char in string with lowest ASCII value
string are really the same object.
max(s) return char in string with highest ASCII value
>>> s1 = str ( " Hello " ) # using the constructor function
>>> s2 = " Hello " # alternative syntax
>>> s3 = str ( " Hello " ) >>> s1 = " Hello , World ! "
>>> s1 is s2 # are these the same object ? >>> len ( s1 )
True 13
>>> s2 is s3 >>> min ( s1 )
True ’ ’
>>> min ( " Hello " )
’H ’
>>> max ( s1 )
’r ’
Why does it make sense for a blank to have lower ASCII value than
any letter?
Texas Summer Discovery Slideset 10: 9 Strings Texas Summer Discovery Slideset 10: 10 Strings
Texas Summer Discovery Slideset 10: 11 Strings Texas Summer Discovery Slideset 10: 12 Strings
Slicing Concatenation and Repetition
General Forms:
Slicing means to select a contiguous
subsequence of a sequence or string. s1 + s2
s * n
General Form: n * s
String[start : end]
s1 + s1 means to create a new string of s1 followed by s2.
>>> s = " Hello , World ! " s * n or n * s means to create a new string containing n
>>> s [1 : 4] # substring from s [1]... s [3] repetitions of s
’ ell ’
>>> s [ : 4] # substring from s [0]... s [3] >>> s1 = " Hello "
’ Hell ’ >>> s2 = " , World ! "
>>> s [1 : -3] # substring from s [1]... s [ -4] >>> s1 + s2 # + is not commutative
’ ello , Wor ’ ’ Hello , World ! ’
>>> s [1 : ] # same as s [1 : s ( len ) ] >>> s1 * 3 # * is commutative
’ ello , World ! ’ ’ Hel loHe lloHello ’
>>> s [ : 5] # same as s [0 : 5] >>> 3 * s1
’ Hello ’ ’ Hel loHe lloHello ’
>>> s [:] # same as s
’ Hello , World ! ’ Notice that concatenation and repetition overload two familiar
>>> s [3 : 1] # empty slice
’’ operators.
Texas Summer Discovery Slideset 10: 13 Strings Texas Summer Discovery Slideset 10: 14 Strings
Texas Summer Discovery Slideset 10: 15 Strings Texas Summer Discovery Slideset 10: 16 Strings
Iterating Over a String Strings are Immutable
Texas Summer Discovery Slideset 10: 17 Strings Texas Summer Discovery Slideset 10: 18 Strings
Texas Summer Discovery Slideset 10: 19 Strings Texas Summer Discovery Slideset 10: 20 Strings
Useful Testing Methods Substring Search
Texas Summer Discovery Slideset 10: 21 Strings Texas Summer Discovery Slideset 10: 22 Strings
Texas Summer Discovery Slideset 10: 23 Strings Texas Summer Discovery Slideset 10: 24 Strings
String Exercise Converting Strings
In file countOverlaps.py:
def countOverlaps ( txt , s ) :
""" Count the occurrences of s in txt , Below are some additional methods on strings. Remember that
including possible overlapping occurrences . """ strings are immutable, so these all make a new copy of the string.
count = 0
while len ( txt ) >= len ( s ) :
if txt . startswith ( s ) :
count += 1
Function Description
txt = txt [1:] s.capitalize(): return a copy with first character capitalized
return count s.lower(): lowercase all letters
s.upper(): uppercase all letters
Running our code: s.title(): capitalize all words
>>> from countOverlaps import * s.swapcase(): lowercase letters to upper, and vice versa
>>> txt = " abababababa " s.replace(old, new): replace occurences of old with new
>>> s = " aba "
>>> countOverlaps ( txt , s )
5
>>>
Texas Summer Discovery Slideset 10: 25 Strings Texas Summer Discovery Slideset 10: 26 Strings
Texas Summer Discovery Slideset 10: 27 Strings Texas Summer Discovery Slideset 10: 28 Strings
String Exercise String Exercise
Exercise: Input a string from the user. Count and print out the Exercise: Input a string from the user. Count and print out the
number of lower case, upper case, and non-letters. number of lower case, upper case, and non-letters.
In file CountCases.py:
def countCases ( txt ) :
""" For a text , count and return the number of lower
upper , and non - letter letters . """
lowers = 0
uppers = 0
nonletters = 0
# For each character in the text , see if lower , upper ,
# or non - letter and increment the count .
for ch in txt :
if ch . islower () :
lowers += 1
elif ch . isupper () :
uppers += 1
else :
nonletters += 1
# Return a triple of the counts .
return lowers , uppers , nonletters
Texas Summer Discovery Slideset 10: 29 Strings Texas Summer Discovery Slideset 10: 30 Strings
Calling countCases
def main () :
txt = input ( " Please enter a text : " )
lc , uc , nl = countCases ( txt )
print ( " Contains : " )
print ( " Lower case letters : " , lc )
print ( " Upper case letters : " , uc )
print ( " Non - letters : " , nl )
main ()