convert integer to string

Chris Rebert crebert at ucsd.edu
Sun Apr 29 13:42:55 EDT 2012


On Sun, Apr 29, 2012 at 10:18 AM, Andres Soto <soto_andres at yahoo.com> wrote:
> I have already
>>>> import string
>>>> from string import *
> but I can not still convert an integer to string

There is no need to import the `string` module to do that. Most of the
`string` module is deprecated.
`str` is the *built-in* type for character strings; so no import is necessary

Also, imports of the form `from X import *` are generally to be avoided.

>>>> str(42)

That should work.

> Traceback (most recent call last):
>   File "<pyshell#20>", line 1, in <module>
>     str(42)
> TypeError: 'module' object is not callable
>>>>
> What is it wrong?

Presumably, at some point you did:
    import string
    str = string
Or similar, thus shadowing the built-in type. As I said, there's no
need to `import string`, and the `str` type is built-in, so both these
lines are unnecessary.

Cheers,
Chris



More information about the Python-list mailing list