Python Cheatsheet 2 – string

This post is a cheatsheet on:

  1. string methods
  2. string formatting

String Method

Index Method Return Notes
Create and encode
1 str(string[, encoding[, errors]]) str
2 .decode(encoding=’utf-8′, errors=’strict’) b’original_str’
3 .encode(encoding=’utf-9′, errors=’strict’) b’original_str’
Format
4 .format(v0, v1, a=v2, …) str(a modified copy) see “String format – Option 2”
5 .format_map(mapping) str(a modified copy)
Justification
6 .center(width[, padding]) str(a modified copy) default padding is whitespace
7 .ljust(width[, padding]) str(a modified copy) default padding is whitespace
8 .rjust(width[, padding]) str(a modified copy) default padding is whitespace
9 .zfill(width) str(a modified copy) fill with “0” on the left
Case
10 .capitalize() str(a modified copy) only capitalize the first letter
11 .lower() str(a modified copy)
12 .upper() str(a modified copy)
13 .swapcase() str(a modified copy)
14 .title() str(a modified copy) The First Letter In Each Word Becomes Capitalized
Count and find
15 .count(str, beg= 0,end=len(string)) int(times of occurence)
16 .(r)find(str, beg=0 end=len(string)) int(first index) if not found raise Error
17 .(r)index(str, beg=0, end=len(string)) int(first index) if not found return -2
“is” method
18 .isalnum() bool return True if str is not empty and “alphanumeric”
19 .isalpha() bool return True if str is not empty and alphabetic
20 .isdigit() bool return True if str is not empty and contains only digits
21 .isdecimal() bool difference see http://www.cnblogs.com/jebeljebel/p/4006433.html
22 .isnumeric() bool
23 .isidentifier() bool
24 .isspace() bool return True if all characters are space
25 .isprintable() bool return True if all characters are printable in repr(), or empty
26 .istitle() bool
27 .isupper() bool
28 .islower() bool
29 .endswith(suffix, beg=0, end=len(string)) bool
30 .startwith(prefix, beg=0, end=len(string)) bool
Manipulation
31 .(l/r/)strip() str(a modified copy)
32 .split(str=” “, maxsplit=string.count(str)) list(of substrings) Do at most “maxsplit” times of split, automaticly remove empty str from the list returned
33 .splitlines(keepends=False) list(of substrings) If keepends=True, leave “/n”s in the list returned.
34 .(r)partition(sep_str) tuple((head, sep_str, tail)) if sep_str is not found return (original_str, “”, “”)
35 .replace(old, new[, count]) str(a modified copy) if “count” is given, only do the first count replacements
36 .expandtabs(tabsize=8) str(a modified copy)
Translation
36 .maketrans(x[, y[, z]]) dict NB
37 .translate(dict_table) str(a modified copy)
Others
38 .join(sequence) str use what’s before the dot to link every repr(elements_of_sequence) into a string

NB:

  • .maketrans() takes 1 to 3 arguments
    1 argument : dict, the mapping has to be from (Unicode ordinals / str) to (Unicode ordinals / str / None)
    2 arguments : iterables, automaticly do zip()
    3 arguments : iterables, zip the first two and map everything in 3 to None
  • Only support Unicode
  • .translate() only takes mapping from Unicode ordinals to Unicode ordinals, strings, or None

String Format

Option 1 : string interpolation

>>> "Overwatch costs %5d or %9.4f" % (198, 328.00)
'Overwatch costs 198 or 328.0000'

Things like “%5d” are placeholders.
The format of placeholders is

%[flags][width][.precision]type

Note that:
1. If the “width” given is too large in the interger part, fill with “flag”(whitespace by default).
2. If the “width” given is too small, it will be automatically change to the smallest legal size.
3. If the “precision” given is smaller than the original number, do rounding (instead of truncating).

Flag format:

Flag Meaning
# Used with o, x or X specifiers the value is preceded with 0, 0o, 0O, 0x or 0X respectively.
0 The conversion result will be zero padded for numeric values.
The converted value is left adjusted
If no sign (minus sign e.g.) is going to be written, a blank space is inserted before the value.
+ A sign character (“+” or “-“) will precede the conversion (overrides a “space” flag).

Conversion format:

Conversion Meaning
d Signed integer decimal.
i Signed integer decimal.
o Unsigned octal.
u Unsigned decimal.
x Unsigned hexadecimal (lowercase).
X Unsigned 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 Same as “e” if exponent is greater than -4 or less than precision, “f” otherwise.
G Same as “E” if exponent is greater than -4 or less than precision, “F” otherwise.
c Single character (accepts integer or single character string).
r String (converts any python object usingrepr()).
s String (converts any python object usingstr()).
% No argument is converted, results in a “%” character in the result.

(Retrieved from http://www.python-course.eu/python3_formatted_output.php)

Option 2 : “.format” method

>>> "Overwatch costs {regular:5d} or {origins:9.4f}".format(regular=198, origins=328)
'Overwatch costs 198 or 328.0000'

Placeholder format is:

{index : [option][width][.precision]type}

It is a very pythonic approach.
Through the customized keys, we get more freedom in arranging the order of values.
Another benefit lies in “option” :

Option Meaning
‘<‘ The field will be left-aligned within the available space. This is usually the default for strings. | | ‘>’ | The field will be right-aligned within the available space. This is the default for numbers.
‘0’ If the width field is preceded by a zero (‘0’) character, sign-aware zero-padding for numeric types will be enabled.
‘,’ This option signals the use of a comma for a thousands separator.
‘=’ Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form “+000000120”. This alignment option is only valid for numeric types.
‘^’ Forces the field to be centered within the available space.

(Retrieved from http://www.python-course.eu/python3_formatted_output.php)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: