Python: seperate string

Discussion in 'Mac Programming' started by lynkynpark86, Dec 7, 2011.

  1. lynkynpark86 macrumors 6502

    So I have a program that generates 2 ints separated by a space (eg '5 93' or '34 115'). I need to set the first to one int, and the second to another. I have no idea how large the numbers would be ('21478 61247' is a possibility, so is '6 3'). How would I do this?
  2. subsonix macrumors 68040

    Feb 2, 2008
    You could try something like this, the fields will be separated into two array indexes and then converted to ints.

    >>> numbers = "123 456"
    >>> array = numbers.split(' ')
    >>> int1 = int(array[0])
    >>> int2 = int(array[1])
    >>> int1
    >>> int2
    You can re-assign the int values to array[0] and array[1] rather than to two new variables if you want to. Don't know what the limit of int is in python, you might have to use long ints instead.
  3. lynkynpark86 thread starter macrumors 6502

    Thanks, I knew I could use split, but I didn't understand the concept at all.

Share This Page