Section 6.5. Built-in Functions


6.5. Built-in Functions

6.5.1. Standard Type Functions

cmp()

As with the value comparison operators, the cmp() built-in function also performs a lexicographic (ASCII value-based) comparison for strings.

>>> str1 = 'abc' >>> str2 = 'lmn' >>> str3 = 'xyz' >>> cmp(str1, str2) -11 >>> cmp(str3, str1) 23 >>> cmp(str2, 'lmn') 0


6.5.2. Sequence Type Functions

len()

>>> str1 = 'abc' >>> len(str1) 3 >>> len('Hello World!') 12


The len() built-in function returns the number of characters in the string as expected.

max() and min()

>>> str2 = 'lmn' >>> str3 = 'xyz' >>> max(str2) 'n' >>> min(str3) 'x'


Although more useful with other sequence types, the max() and min() built-in functions do operate as advertised, returning the greatest and least characters (lexicographic order), respectively. Here are a few more examples:

>>> min('ab12cd') '1' >>> min('AB12CD') '1' >>> min('ABabCDcd') 'A'


enumerate()

>>> s = 'foobar' >>> for i, t in enumerate(s): ...  print i, t ... 0 f 1 o 2 o 3 b 4 a 5 r


zip()

>>> s, t = 'foa', 'obr' >>> zip(s, t) [('f', 'o'), ('o', 'b'), ('a', 'r')]


6.5.3. String Type Functions

raw_input()

The built-in raw_input() function prompts the user with a given string and accepts and returns a user-input string. Here is an example using raw_input():

>>> user_input = raw_input("Enter your name: ") Enter your name: John Doe >>> >>> user_input 'John Doe' >>> >>> len(user_input) 8


Earlier, we indicated that strings in Python do not have a terminating NUL character like C strings. We added in the extra call to len() to show you that what you see is what you get.

str() and unicode()

Both str() and unicode() are factory functions, meaning that they produce new objects of their type respectively. They will take any object and create a printable or Unicode string representation of the argument object. And, along with basestring, they can also be used as arguments along with objects in isinstance() calls to verify type:

>>> isinstance(u'\0xAB', str) False >>> not isinstance('foo', unicode) True >>> isinstance(u'', basestring) True >>> not isinstance('foo', basestring) False


chr(), unichr(), and ord()

chr() takes a single integer argument in range(256) (e.g., between 0 and 255) and returns the corresponding character. unichr() does the same thing but for Unicode characters. The range for unichr(), added in Python 2.0, is dependent on how your Python was compiled. If it was configured for UCS2 Unicode, then a valid value falls in range(65536) or 0x0000-0xFFFF; for UCS4, the value should be in range(1114112) or 0x000000-0x110000. If a value does not fall within the allowable range(s), a ValueError exception will be raised.

ord() is the inverse of chr() (for 8-bit ASCII strings) and unichr() (for Unicode objects)it takes a single character (string of length 1) and returns the corresponding character with that ASCII code or Unicode code point, respectively. If the given Unicode character exceeds the size specified by your Python configuration, a TypeError exception will be thrown.

>>> chr(65) 'A' >>> ord('a') 97 >>> unichr(12345) u'\u3039' >>> chr(12345) Traceback (most recent call last):   File "<stdin>", line 1, in ?     chr(12345) ValueError: chr() arg not in range(256) >>> ord(u'\ufffff') Traceback (most recent call last):   File "<stdin>", line 1, in ?     ord(u'\ufffff') TypeError: ord() expected a character, but string of length 2 found >>> ord(u'\u2345') 9029




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net