String to int newbie question

Luka Milkovic Luka.Milkovic at
Sun Mar 23 01:22:14 CET 2003

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