Computer Science 15-112, Fall 2011
Class Notes:
Strings
Strings
string.ascii_letters | 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
string.ascii_lowercase | 'abcdefghijklmnopqrstuvwxyz' |
string.ascii_uppercase | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
string.digits | '0123456789' |
string.hexdigits | '0123456789abcdefABCDEF' |
string.letters | See documentation for details. |
string.lowercase | 'abcdefghijklmnopqrstuvwxyz' (on most systems) |
string.octdigits | '01234567' |
string.punctuation | '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' |
string.printable | digits + letters + punctuation + whitespace |
string.uppercase | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' (on most systems) |
string.whitespace | space + tab + linefeed + return + formfeed + vertical tab (on most systems) |
# You cannot change strings! They are immutable.
s = "abcde"
s[2] = "z" # Error! Cannot assign into s[i]
# Instead, you must create a new string
# But... This is inefficient! (More on this next week, once we cover lists...)
s = "abcde"
s = s[:2] + "z" + s[3:] # This is inefficient (if inside a tight loop), but at least it works
print s # prints abzde
Flag | Meaning |
---|---|
'#' | The value conversion will use the “alternate form” (where defined below). |
'0' | The conversion will be zero padded for numeric values. |
'-' | The converted value is left adjusted (overrides the '0' conversion if both are given). |
' ' | (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. |
'+' | A sign character ('+' or '-') will precede the conversion (overrides a “space” flag). |
Conversion | Meaning |
---|---|
'd' | Signed integer decimal. |
'i' | Signed integer decimal. |
'o' | Signed octal value. |
'u' | Obsolete type – it is identical to 'd'. |
'x' | Signed hexadecimal (lowercase). |
'X' | Signed hexadecimal (uppercase). |
'e' | Floating point exponential format (lowercase). |
'E' | Floating point exponential format (uppercase). |
'f' | Floating point decimal format. |
'F' | Floating point decimal format. |
'g' | Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. |
'G' | Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. |
'c' | Single character (accepts integer or single character string). |
'r' | String (converts any Python object using repr()). |
's' | String (converts any Python object using str()). |
'%' | No argument is converted, results in a '%' character in the result. |
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem