Strings
Strings
• A string is a sequence of characters.
• A string can have 0 characters (“”) or it can be as big as the memory on the computer running your program.
Character Sets• There are 2 basic sets of characters in the
computer world
• ASCII– 8 bits– size of set is 2 ** 8 – big enough
• Unicode– 32 bits– Size of set is 2 ** 32 – big!!!!!
• ASCII character set
– http://www.asciitable.com/
• Unicode is pretty complex.– don't go there until you have to.
Assigning Strings to Variables
• Make a variable hold a string by enclosing the characters to be assigned in double or single quotes.
e.g. userName = “billy”• Note that:
aNumber = “129”
• Is not the same as
aNumber = 129
Converting Strings
• Convert a string to an int
aNumber = int(aString) or
aNumber = int(“562”)• Convert a int to a string
aString = str(aNumber)• There's a float() version of int(), too
Indexing Strings• You can pick out individual characters from a string.>>> someStr = "Billy"
>>> print someStr[0]
B
>>> print someStr[1]
i
>>> print someStr[2]
l
>>> print someStr[3]
l
>>> print someStr[4]
y
>>> print someStr[5]
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
print someStr[5]
IndexError: string index out of range
>>>
Indexing Strings
• String indexes start at 0 (not at 1)• If you try to access a string at an index
that is too high, you generate an IndexError.
String Formatting
• Format Specifiers: special character sequences inside strings that are meant to be replaced by other strings or floats or integers
fName = “Billy”
print “Hello %s” % fName
>>> Hello Billy
String Formatting (cont’d)
lName = Billson
print “Hello %s %s” % (fName, lName)
>>> Hello Billy Billson• The format specifiers are like placeholders
which are replaced by the variables inside the brackets
This thing is called a tuple
String Formatting (cont’d)
• The order of the variables in the tuple controls the order in which they appear in the string
print “Hello %s %s” % (lName, fName)
>>> Hello Billson Billy
String Formatting (cont’d)
• The items in the tuple don’t have to be variables
print “Hello %s %s” % (“Mr.”, “Mac”)
>>> Hello Mr. Mac• …but they almost always are…
String Formatting (cont’d)
• There are format specifiers for integers as well.
age = 1052
print “%s %s is %d years old” % \ (“Mr.”, “Mac”, age)
>>> Mr. Mac is 1052 years old
• For integers we use %d
String Formatting (cont’d)
• There are format specifiers for float/decimal values too
discount = 1052.97 * .094538print “Discount: $%f” % discount>>> Discount: $99.545678
• Since this represents money, 2 decimal places would be better:
print “Discount: $%.2f % discount>>> Discount $99.55
String Formatting (cont’d)
• We can also add padding to a string>>>print "X%20sX" % fNameX billyX• Python adds enough spaces to make fName be
twenty chars wide• fName is right-justified in this example. Want
left-justified? Use a ‘-’ sign.>>> print “X%-20sX” % fNameXbilly X
String Methods
• The following methods can be used on any string. Assume we have a string called s.
• s.center(n)
• produces a new string with s centered in a field of width n. The field is padded with spaces if n is greater than the length of s.
>>> knight = "Nee“
>>> print "X%sX" % knight.center(10)
X Nee X
Stripping White Space
• s.strip() returns s with any leading or trailing whitespace (tabs, newlines, or spaces) removed
• s.lstrip() – as above but only the leading whitespace is removed
• s.rstrip() – as above but only the trailing whitespace is removed
strip() examples
>>> knight = " Nee "
>>> print "X%sX" % knight.strip()
XNeeX
>>> print "X%sX" % knight.lstrip()
XNee X
>>> print "X%sX" % knight.rstrip()
X NeeX
>>>
Lower & Upper Case
• s.lower() and s.upper() convert s to lower and upper case, respectively
>>> print name.upper()
BILLY
>>> print name.lower()
billy
>>>
Ex2.6 - alignIt
• Ask the user for 5 first and last names.• Use string formatting to print out the first and last
names (one per line) so that the last names are all lined up (table format) underneath each other.
Billy BillsonMargaret WilsonDmitry DillsonKay KillsonYolanda Yillson
Ex 2.7 boxIt.py
• Pgm asks user to put in 5 strings• Pgm converts strings to upper case• Pgm strips any leading or trailing
whitespace• Pgm then prints all 5 strings left justifed in
a box of asterisks.• Box must have a row of asterisks at the
top and at the bottom.
Ex 2.4a boxIt.py (advanced)IGNORE this PLS
• Talk to me for some extras needed for this exercise. (It’s complicated!)
• User types in 5 strings• Pgm loops until user enters 5 unique strings• Unique means regardless of whitespace or
capitalization• Pgm then prints all 5 strings left justifed in a box
of asterisks.• Box must have a row of asterisks at the top and
at the bottom.
Escape sequences
• Putting a backslash “\” before some characters gives them special meanings.
• “\n” - means print a new line
• “\t” - means print a tab>>> print "Billy is a\nbad,bad\t\t\tboy"
Billy is a
bad,bad boy
>>>
More escape sequences
• If “\” has a special meaning when it’s in front of an “n” or a “t” (& other things), how do we actually print a “\”?
>>> print "Billy is a \\nasty boy"
Billy is a \nasty boy
>>>
More escape sequences
• Use \’ to print a single quote• Use \” to print a double quote
>>> print "Billy\'s a \"rude\" boy"
Billy's a "rude" boy
>>>