Python: Strings - Methods

Python: Strings - Methods ? As discussed earlier, strings are sequences of characters delimited by single or

double quotes ? String methods are associated with the String class, and as such are associated

with each individual string object ? These methods are accessed using the syntax

? These do not affect the string itself ? Built-in methods:

1. Construction/exploding operations (a) join ? Syntax:

? Semantics: Creates a string made from string-arg parameters separated by string

? Example: ":".join(['hours', 'minutes']) 'hours:minutes' (b) split

? Syntax:

? Semantics: Creates a list of strings created by breaking string apart Essentially the opposite of join If no arguments are provided, the splits occur on any white space If the string argument is provided, the splits occur on that string If the int argument is provided, that number of splits occur on the provided string

1

Python: Strings - Methods (2) ? Examples:

"Every good boy does fine".split() ['Every', 'good', 'boy', 'does', 'fine'] "15:21:00".split(":") ['15', '21', '00'] "15:21:00".split(":", 1) ['15', '21:00'] 2. Formatting operations (a) strip ? Syntax:

? Semantics: Creates a string made by removing characters from the begining and/or end of string By default (no argument), white space is removed from the ends of the string If strip string is included, any characters included in strip string are removed

? Examples: " hello ".strip() 'hello' "abc-keep-axa".strip("abcde-") 'keep-ax'

(b) lstrip ? Syntax: See strip ? Semantics: Same as strip but only affects beginning of string

(c) rstrip ? Syntax: See strip ? Semantics: Same as strip but only affects end of string

2

Python: Strings - Methods (3) (d) maketrans and translate

? These are used in tandem for translating/encoding characters ? First, a table is generated using maketrans:

Syntax:

Example: t = "abcde".maketrans("bd", ":;") The result must be saved to be used by translate ? Then, the table is used to convert the source chars to code chars by translate: Syntax:

Example: "bbbddd".translate(t) ':::;;;' Note that the string used in making the table does not need to

be the string that the table is applied to, nor do the characters in source chars need to be contained in the string used when making the table (e) lower ? Syntax:

? Semantics: Converts string to lower case (f) upper

? Syntax:

? Semantics: Converts string to upper case (g) capitalize

? Syntax:

? Semantics: Capitalizes the first character of string

3

(h) title ? Syntax:

Python: Strings - Methods (4)

? Semantics: Capitalizes the first character of each word of string (i) swapcase

? Syntax:

? Semantics: Changes the case of each letter in string (j) ljust

? Syntax:

? Semantics: Left justifies string in a field of field width characters padded with spaces

? Example: "abc".ljust(8) 'abc ' (k) rjust

? Syntax:

? Semantics: Right justifies string in a field of field width characters padded with spaces

? Example: "abc".rjust(8) ' abc' (l) center

? Syntax:

? Semantics: Centers string in a field of field width characters padded with spaces

? Example: "abc".center(11) ' abc '

4

(m) zfill ? Syntax:

Python: Strings - Methods (5)

? Semantics: Adds zeroes to the left of string in a field of field width ? Example: "1234".zfill(8) '00001234' 3. Query operations (a) find ? Syntax:

? Semantics: Returns index of the first occurrence of key in string working left to right If start is included, searching starts at position start If end is included, searching ends immediately before position end If key is not found, -1 is returned

? Examples: "singsing".find("in") 1

"singsing".find("in", 2) 5

"singsing".find("in", 2, 6) -1

(b) rfind ? Syntax: See find ? Semantics: Same as find, but returns index of the last occurrence of key in string working right to left

(c) index ? Syntax:

? Semantics: Same as find, but returns an exception instead of -1 when key not found

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download