Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

convert integer to string

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


soto_andres at yahoo

Apr 29, 2012, 10:18 AM

Post #1 of 3 (198 views)
Permalink
convert integer to string

I have already
>>> import string

>>> from string import *

but I can not still convert an integer to string
>>> str(42)
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    str(42)
TypeError: 'module' object is not callable
>>> 
What is it wrong?
Thank you
 
Prof. Dr. Andrés Soto
DES DACI
UNACAR


python at mrabarnett

Apr 29, 2012, 10:36 AM

Post #2 of 3 (189 views)
Permalink
Re: convert integer to string [In reply to]

On 29/04/2012 18:18, Andres Soto wrote:
> I have already
>> >> import string
>> >> from string import *
> but I can not still convert an integer to string
>> >> str(42)
> Traceback (most recent call last):
> File "<pyshell#20>", line 1, in <module>
> str(42)
> TypeError: 'module' object is not callable
>> >>
> What is it wrong?
>
At some point you have bound "str" to a module.

Normally you would be getting this:

>>> str is __builtins__.str
True
--
http://mail.python.org/mailman/listinfo/python-list


crebert at ucsd

Apr 29, 2012, 10:42 AM

Post #3 of 3 (188 views)
Permalink
Re: convert integer to string [In reply to]

On Sun, Apr 29, 2012 at 10:18 AM, Andres Soto <soto_andres [at] yahoo> 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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.