Computer Science 15-112, Fall 2011
Class Notes: Strings
Excerpts from:
Python
Standard Library, Ch 2 (Built-In Functions)
(just these String-related functions: bin, chr, eval, hex, len, oct,
ord, raw_input, repr, reversed, str)
The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order.
New in version 2.6.
Changed in version 2.4: formerly locals was required to be a dictionary.
The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard __builtin__ module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:
>>> x = 1
>>> print eval('x+1')
2
This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case pass a code object instead of a string. If the code object has been compiled with 'exec' as the mode argument, eval()‘s return value will be None.
Hints: dynamic execution of statements is supported by the exec statement. Execution of statements from a file is supported by the execfile() function. The globals() and locals() functions returns the current global and local dictionary, respectively, which may be useful to pass around for use by eval() or execfile().
See ast.literal_eval() for a function that can safely evaluate strings with expressions containing only literals.
Note
To obtain a hexadecimal string representation for a float, use the float.hex() method.
Changed in version 2.4: Formerly only returned an unsigned literal.
Changed in version 2.4: Formerly only returned an unsigned literal.
raw_input([prompt])
>>> s = raw_input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
If the readline module was loaded, then raw_input() will use it to provide elaborate line editing and history features.
New in version 2.4.
Changed in version 2.6: Added the possibility to write a custom __reversed__() method.
For more information on strings see Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange which describes sequence functionality (strings are sequences), and also the string-specific methods described in the String Methods section. To output formatted strings use template strings or the % operator described in the String Formatting Operations section. In addition see the String Services section. See also unicode().
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem