6. More definitions
• Using single or double is fine:
– S = “a string”
– S = ‘a string’
• Don’t use both!
– S = “a string’ WRONG!
• How to put in a apostrophe?
– S = “Mike’s string” #Use different quotes
– S = ‘Mike’s string’ #Use an escape character
Module 8 : Strings and Characters 6
24. Getting the code
• ord() takes a string of length 1 as input and
will return the unicode of the character (for
standard symbols this is same as ASCII)
• chr() takes an ASCII code and returns a string
on length 1 containing the corresponding
character
ord(‘a’) => 97
chr(97) => ‘a’
Encrpyt (see later):
code = ord(‘a’)
chr(code+1) => ‘b’
Module 8 : Strings and Characters 24 of 53
29. Strings are Immutable
– aStr = ‘spam’
– aStr[1] = ‘l’ ERROR
• However, you can use it to make another
string (copy it, slice it, etc).
– newStr = aStr[:1] + ‘l’ + aStr[2:]
– aStr ‘spam’
– newStr => ‘slam’
Module 8 : Strings and Characters
• Strings are immutable, that is
you cannot change one once
you make it:
29
35. Methods in general
• We’ll touch on this more later.
• But in general the form is:
– object.method()
• We say object is calling the method method.
• Different objects have different methods available,
defined by the type (class).
• How to find out all methods available on strings? Use a
reference! : Python online…
– http://docs.python.org/lib/string‐methods.html
• Iintegrated Development Environment (IDE) such as
IDLE will help.
Module 8 : Strings and Characters 35
36. Find
myStr = “Find in a string”
myStr.find(‘d’) => 3
• find is another string method, called using
same object.method() notation.
• It takes a single character string as input and
returns the index of the position where the
character is first seen (from left to right).
• ‘d’ is called an argument of the method.
Module 8 : Strings and Characters 36
43. Thought process
• Common? No
• What do we have available to us?
• A method…
• Iterate through string, check if its in second
half of string (using len()) and re‐build,
changing case where necessary
• Alternatively, split the string in half first.
Module 8 : Strings and Characters 43
46. Basic form
• Look at an example:
n = 500
description = “a lot!”
print(“In this class we have {}
students, which is {}”.format(n,
description))
• This will show:
In this class we have 500 students, which is a lot!
Module 8 : Strings and Characters 46
47. Basic form
• format creates a new string!
“In this class we have {} students,
which is {}”.format(n, description)
• This outputs a new string where {} will be
replaced by the data inside the format
method (this case n and description)
Module 8 : Strings and Characters 47
48. Example
print(“In this class we have {} students, which is {}”.format(n, description))
String in quotes
format
• Objects are matched in order with format
descriptors. The substitution is made and resulting
string created (then printed)
In this class we have 500 students, which is a lot!
Module 8 : Strings and Characters 48
50. Descriptors (codes)
• s string
• d decimal
• e floating point exponent
• f floating point decimal
• u unsigned integer
Module 8 : Strings and Characters 50
51. width and alignment
• < left
• > right
• ^ center
• Integers indicate width
print(‘{:>10s} is {:<10d} years old.’.format(‘Bill’, 25))
• ‘Bill’ is printed right aligned as a string, with at least
10 spaces.
Module 8 : Strings and Characters 51
52. Example of space
print(“MIKE, OFFICE: {:3d} TEL: {:<10d} ”.format(76, 67906277))
MIKE, OFFICE: 76 TEL:67906277
3 spaces wide,
right justified
(by default)
10 spaces wide,
left justified
Module 8 : Strings and Characters 52
53. Precision
• print(math.pi)
3.14159265359 #default is 11
• print('{:.4f}'.format(math.pi))
3.1416 (4 decimal places of precision,
with rounding)
• print('{:10.2f}'.format(math.pi))
• ‘ 3.14’ (10 spaces for
characteristic and the decimal point – not
including mantissa)
Module 8 : Strings and Characters 53