String to int newbie question

Ivan Georgiev yngwiie at developer.bg
Sat Mar 22 20:13:09 EST 2003


Hallo Luka, I am a newbie as well, but still here is my view of the probl=
em.
I do not know what you are trying to do exactly, but
if you know the max. number you are going ot get,  then you know
how many zeros you are going to have to add in the back conversion to string.
if max is. 99999 then every other number would have to have that many zer
as to conatin 5 chars.
00009
00099
00999 etc.
So you can simply add as many zeros as you need in the back conversion.

Regards:
Ivan

> On Sat, 22 Mar 2003 14:44:55 +0100, Rene Pijlman wrote:
> > Boris Genz:
> >>I want to convert a number string ( '3211' for example ) to a number, (
> >>actually integer ) preserving the original number
> >
> > Is there any other way?
> >
> >>( the exact number being string and integer )
> >
> > Que?
> >
> >>but if my number string starts with one or more zeroes, then the numbers
> >>won't be same...
> >
> > Yes they will. '0027' and '027' both convert to 27.
> >
> >>For example, if I have string = '0027' then int(string) would give 27.
> >>How can I add leading zeroes to that number?
> >
> > By not converting it to an integer. It already had leading zeroes before
> > you converted it :-)
> >
> > 0027 == 027 == 27
>
> Ok, I proabably haven't been clear enough. Here is what I really want to
> do: I have a list of strings ( the content of strings are actually
> numbers ), for example I have the following list: ['4312', '7599',
> '0724', '0003']. The things I need to do is to change the strings inside
> the list to integers and then write the list to a file ( I know how to do
> the latter:). The problem is, if I change it with
> int(my_string_inside_the_list) I won't get the same numbers, and I need
> to have the content of the string and integer the same. So, after the
> conversion of that list, what I want to have is: [4312, 7599, 0724, 0003]
> and NOT [4312, 7599, 724, 3].
> Can you solve my problem?
> Your help will be appreciated.
> Thank you






More information about the Python-list mailing list