[Tutor] Question: Converting ints to strings

Wesley J. Chun wesc@alpha.ece.ucsb.edu
Mon, 24 Apr 2000 00:29:31 -0700 (PDT)


    > From: "Fiel Cabral" <fcabral@i-next.net>
    > Date: Mon, 24 Apr 2000 14:24:23 -0400
    > 
    > Please tell me how to convert an int to a string.
    > I'm trying to do the following:
    > 
    >     block_number = 1
    >     file_name = 'temp.file'
    >     output = open(file_name + '.$' + block_number + '$', 'wb')
    > 
    > This didn't work. I was hoping that it would create 
    > a file called 'temp.file.$1$'.


there are (at least) two ways accomplishing this.  in your
situation, you can either use the string format operator
(%) like this...

output = open('%s.$%d$' % (file_name, block_number), 'wb')
(%s indicates a string and %d indicates an integer)

... or you can directly convert the number (or any object)
to a string using the str() built-in function, like this:

output = open(file_name + '.$' + str(block_number) + '$', 'wb')

hope this helps!!

-wesley

"Core Python Programming", Prentice-Hall, TBP Summer 2000

wesley.j.chun :: wesc@alpha.ece.ucsb.edu
cyberweb.consulting :: silicon.valley, ca
http://www.roadkill.com/~wesc/cyberweb/