15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

string.split(str="", num=string.count(str)) Splits string according to delimiter str (space if<br />

not provided) and returns list of substrings; split<br />

into at most num substrings if given<br />

string.splitlines(num=string.count('\n')) [b] [c] Splits string at all (or num) NEWLINEs and returns<br />

,<br />

a list of each line with NEWLINEs removed<br />

string.startswith(obj, beg=0, end=len(string))<br />

[b] , [e]<br />

Determines if string or a substring of string (if<br />

starting index beg and ending index end are<br />

given) starts with obj where obj is typically a<br />

string; if obj is a tuple, then any of the strings in<br />

that tuple; returns true if so, and False otherwise<br />

string.strip([obj]) Performs both lstrip() and rstrip() on string<br />

string.swapcase() Inverts case for all letters in string<br />

string.title() [b] [c] Returns "titlecased" version of string, that is, all<br />

,<br />

words begin with uppercase, and the rest are<br />

lowercase (also see istitle())<br />

string.translate(str, del="") Translates string according to translation table<br />

str (256 chars), removing those in the del string<br />

string.upper() Converts lowercase letters in string to uppercase<br />

string.zfill(width) Returns original string left-padded with zeros to<br />

a total of width characters; intended for numbers,<br />

zfill() retains any sign given (less one zero)<br />

[a] Applicable to Unicode strings only in 1.6, but to all string types in 2.0.<br />

[b] Not available as a string module function in 1.5.2.<br />

[e] New or updated in <strong>Python</strong> 2.5.<br />

[c] New in Jython 2.1.<br />

[d] Applicable to Unicode strings only.<br />

Some examples of using string methods:<br />

>>> quest = 'what is your favorite color?'<br />

>>> quest.capitalize()<br />

'What is your favorite color?'<br />

>>><br />

>>> quest.center(40)<br />

' what is your favorite color? '<br />

>>><br />

>>> quest.count('or')<br />

2<br />

>>><br />

>>> quest.endswith('blue')<br />

False

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!