See All Titles |
![]() ![]() Built-in FunctionsStandard Type Functionscmp()As with the value comparison operators, the cmp() built-in function also performs a lexicographic comparison for strings. >>> str1 = 'abc' >>> str2 = 'lmn' >>> str3 = 'xyz' >>> cmp(str1, str2) -11 >>> cmp(str3, str1) 23 >>> cmp(str2, 'lmn') 0 Sequence Type Functionslen()>>> 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. String Type Function [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.
|
© 2002, O'Reilly & Associates, Inc. |