Slides8 Strings Nup
Slides8 Strings Nup
One of the most useful Python data types is the string type,
CS303E: Elements of Computers defined by the str class. Strings are actually sequences of
characters.
and Programming
More on Strings Strings are immutable, meaning you can’t change them after they
are created.
All immutable objects with the same content are stored as one
object.
Strings have some associated special syntax:
>>> s1 = str ( " Hello " ) # using the constructor function
>>> s2 = " Hello " # alternative syntax
>>> id ( s1 ) # strings are unique
1 39 86 4 25 5 46 44 2 4
>>> id ( s2 )
1 39 86 4 25 5 46 44 2 4
>>> s3 = str ( " Hello " )
>>> id ( s3 )
1 39 86 4 25 5 46 44 2 4
>>> s1 is s2 # are these the same object ?
True
>>> s2 is s3
True
In Slideset 5, we had code to compute and print a multiplication The in and not in operators allow checking whether one string is
table up to LIMIT - 1, a contiguous substring of another.
> python Mu l t i p l i c a t i o n T a b l e . py
Multiplicatio n Table General Forms:
| 1 2 3 4 5 6 7 8 9 s1 in s2
------------------------------------------
1 | 1 2 3 4 5 6 7 8 9 s1 not in s2
2 | 2 4 6 8 10 12 14 16 18
....
>>> s1 = " xyz "
9 | 9 18 27 36 45 54 63 72 81
>>> s2 = " abcxyzrls "
>>> s3 = " axbyczd "
which included: >>> s1 in s2
True
print ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) >>> s1 in s3
False
That works well for LIMIT = 10, but not otherwise. How could >>> s1 not in s2
False
you fix it? >>> s1 not in s3
print ( " ------ " + " ----" * ( LIMIT - 1) ) True
def swapCaseWrong ( s ) :
for i in range ( len ( s ) ) : You can’t change a string, by assigning at an index. You have to
if ( ’A ’ <= s [ i ] <= ’Z ’ ) : create a new string.
s [ i ] = chr ( ord ( s [ i ]) + DIFF )
elif ( ’a ’ <= s [ i ] <= ’z ’ ) :
s [ i ] = chr ( ord ( s [ i ]) - DIFF )
return s >>> s = " Pat "
>>> s [0] = ’R ’
print ( swapCaseWrong ( " abCDefGH " ) ) Traceback ( most recent call last ) :
File " < stdin > " , line 1 , in < module >
TypeError : ’ str ’ object does not support item assignment
> python StringIterate . py >>> s2 = ’R ’ + s [1:]
Traceback ( most recent call last ) : >>> s2
File " StringIterate . py " , line 38 , in < module > ’ Rat ’
print ( swapCaseWrong ( " abCDefGH " ) )
File " StringIterate . py " , line 35 , in swapCaseWrong
s [ i ] = chr ( ord ( s [ i ]) - DIFF )
TypeError : ’ str ’ object does not support item assignment
Whenever you concatenate two strings or append something to a
string, you create a new value. Don’t forget to save it!
What went wrong?
Function Description
s.isalnum(): nonempty alphanumeric string?
s.isalpha(): nonempty alphabetic string?
s.isdigit(): nonempty and contains only digits?
s.isidentifier(): follows rules for Python identifier?
s.islower(): nonempty and contains only lowercase letters?
s.isupper(): nonempty and contains only uppercase letters?
s.isspace(): nonempty and contains only whitespace?
Notice that these are methods of class str, not functions, so must
be called on a string s.
>>> islower ( " xyz " )
Traceback ( most recent call last ) :
File " < stdin > " , line 1 , in < module >
NameError : name ’ islower ’ is not defined
When your program accepts input from the user, it’s always a good When your program accepts input from the user, it’s always a good
idea to “validate” the input. idea to “validate” the input.
Earlier in the semester, we wrote: Earlier in the semester, we wrote:
# See if an integer entered is prime . # See if an integer entered is prime .
num = int ( input ( " Enter an integer : " ) ) num = int ( input ( " Enter an integer : " ) )
< code to test if num is prime > < code to test if num is prime >
What’s ’wrong’ with this code? What’s ’wrong’ with this code?
If the string entered does not represent an integer, int might fail.
>>> num = int ( input ( " Enter an integer : " ) )
Enter an integer : 3.4
Traceback ( most recent call last ) :
File " < stdin > " , line 1 , in < module >
ValueError : invalid literal for int () with base 10: ’ 3.4 ’
This still isn’t quite right. Can you see what’s wrong? This still isn’t quite right. Can you see what’s wrong?
It doesn’t allow +3, but does allow 0. How would you fix it?
Function Description
s.lstrip(): return copy with leading whitespace removed >>> ans = input ( " Please enter YES or NO : " )
s.rstrip(): return copy with trailing whitespace removed Please enter YES or NO : NO
>>> ans
s.strip(): return copy with leading and trailing whitespace removed ’ NO ’
>>> ans == ’ YES ’ or ans == ’ NO ’
>>> s1 = " abc " False
>>> s1 . lstrip () # new string >>> ans = input ( " Please enter YES or NO : " ) . strip ()
’ abc ’ Please enter YES or NO : YES
>>> s1 . rstrip () # new string >>> ans
’ abc ’ ’ YES ’
>>> s1 . strip () # new string >>> ans == ’ YES ’ or ans == ’ NO ’
’ abc ’ True
>>> " a b c " . strip () >>>
’a b c ’
Recall from Slideset 3, our functions for formatting strings. The In Slideset 5, we had code to compute and print a multiplication
str class also has some formatting options: table up to LIMIT - 1.
Function Description > python M u l t i p l i c at i o n T a b l e . py
s.center(w): returns a string of length w, with s centered Multi plicatio n Table
| 1 2 3 4 5 6 7 8 9
s.ljust(w): returns a string of length w, with s left justified ------------------------------------------
s.rjust(w): returns a string of length w, with s right justified 1 | 1 2 3 4 5 6 7 8 9
...
s = " abc "
>>> s . center (10) # new string which included the following code to center the title:
’ abc ’
>>> s . ljust (10) # new string print ( " Mul tiplicat ion Table " )
’ abc ’
>>> s . rjust (10) # new string
’ abc ’ A better way would be:
>>> s . center (2) # new string
print ( " Mult iplication Table " . center (6 + 4 * ( LIMIT -1) ) )
’ abc ’
With LIMIT = 10: A comma-separated values (csv) file is a common way to record
> python Mu l t i p l i c a t i o n T a b l e . py data. Each line has multiple values separated by commas. For
Multiplicatio n Table example, I can download your grades from Canvas in csv format:
| 1 2 3 4 5 6 7 8 9
------------------------------------------
1 | 1 2 3 4 5 6 7 8 9
Name , EID , HW1 , HW2 , Exam1 , Exam2 , Exam3
2 | 2 4 6 8 10 12 14 16 18 Possible , ,10 ,10 ,100 ,100 ,100
... Jones ; Bob , bj123 ,10 ,9 ,99 ,60 ,45
9 | 9 18 27 36 45 54 63 72 81
Riley ; Frank , fr498 ,4 ,8 ,72 ,95 ,63
With LIMIT = 13: Smith ; Sally , ss324 ,5 ,10 ,100 ,75 ,80
> python Mu l t i p l i c a t i o n T a b l e . py
Multi plicatio n Table Suppose you needed to process such a file. There’s an easy way to
| 1 2 3 4 5 6 7 8 9 10 11 12
------------------------------------------------------ extract that data (the Python string split method), which we’ll
1 | 1 2 3 4 5 6 7 8 9 10 11 12 cover soon.
2 | 2 4 6 8 10 12 14 16 18 20 22 24
... But suppose you needed to write your own functions to extract the
12 | 12 24 36 48 60 72 84 96 108 120 132 144
data from a line.
Later we’ll explain how to process files. For now, let’s process a
line.
>>> from FieldToComma2 import *
In file FieldToComma2.py: >>> line = " abc , def ,ghi , jkl "
def SplitOnComma ( str ) : >>> first , rest = SplitOnComma ( line )
""" Given a string possibly containing a comma , >>> first
return the initial string ( before the comma ) and ’ abc ’
the string after the comma . If there is no comma , >>> rest
return the string and the empty string . """ ’ def ,ghi , jkl ’
if ( ’ , ’ in str ) : >>> first , rest = SplitOnComma ( rest )
index = str . find ( " ," ) >>> first
# Note : returns a pair of values ’ def ’
return str [: index ] , str [ index +1:] >>> rest
else : ’ghi , jkl ’
return str , " "
Notice that this returns a pair of values. How would you split on
something other than a comma?
String Example