[Tutor] Breaking down integers?
Brian van den Broek
bvande at po-box.mcgill.ca
Mon Jan 3 23:41:49 CET 2005
kilovh said unto the world upon 2005-01-03 17:17:
> I would like to be able to take an integer, break it down into
> individual items in a list, and then put them back together. I know
> how to do this last part thanks to Orri Ganel and Guillermo
> Fernandex, but what about taking the integer apart?
>
> Sorry if the questions have incredibly obvious answers, I'm new to
> this.
>
> ~kilovh
>
Hi,
I'm not sure I've the intent of your question, but do you mean something
like this:
.>>> an_int = 42
.>>> an_int_as_string = str(an_int)
.>>> print an_int_as_string
42
.>>> type(an_int_as_string)
<type 'str'>
.>>> an_int_as_string_list = list(an_int_as_string)
.>>> print an_int_as_string_list
['4', '2']
.>>> rebuilt_int_string = ''.join(an_int_as_string_list)
.>>> print rebuilt_int_string
42
.>>> type(rebuilt_int_string)
<type 'str'>
.>>> rebuilt_int = int(rebuilt_int_string)
.>>> print rebuilt_int
42
.>>> type(rebuilt_int)
<type 'int'>
.>>>
Does that help?
Best to all,
Brian vdB
More information about the Tutor
mailing list