{article Dive into Python}{title} {text}{/article}

The str coerces data into a string. Every datatype can be coerced into a string.

.Example

str(1)

horsemen = ['war', 'pestilence', 'famine']
horsemen

horsemen.append('Powerbuilder')
str(horsemen)

import odbchelper
str(odbchelper)

str(None)

  1. >>> str(1)
    '1'
    >>> horsemen = ['war', 'pestilence', 'famine']
    >>> horsemen
    ['war', 'pestilence', 'famine']
    >>> horsemen.append('Powerbuilder')
  2. >>> str(horsemen)
    "['war', 'pestilence', 'famine', 'Powerbuilder']"
  3. >>> import odbchelper
    >>> str(odbchelper)
    "<module 'odbchelper' from 'C:\\\\Python34\\\\odbchelper.py'>"
  4. >>> str(None)
    'None'

  5. 1: For simple datatypes like integers, you would expect str to work, because almost every language has a function to convert an integer to a string.

    2: However, str works on any object of any type. Here it works on a list which you've constructed in bits and pieces.

    3: str also works on modules. Note that the string representation of the module includes the pathname of the module on disk, so yours will be different.

    4: A subtle but important behavior of str is that it works on None, the Python null value. It returns the string 'None'. You'll use this to your advantage in the info function, as you'll see shortly.
    At the heart of the info function is the powerful dir function. dir returns a list of the attributes and methods of any object: modules, functions, strings, lists, dictionaries... pretty much anything.