Reputation: 11486
I want to convert num1 and num2 to a list and print it,any inputs on how this can be done?
num1 = 12345
num2 = 34266 73628
print num1
print num2
EXPECTED OUTPUT:-
['12345']
['34266','73628']
Upvotes: 2
Views: 143
Reputation: 10727
num1 = '12345'
num2 = '12345 67890'
#Note: Both must be strings
#Option 1(Recommended)
print num1.split()
print num2.split()
#Option 2
import shlex
print shlex.split(num2)
#Option 3
import re
print re.split(' ', num2)
#If the array needs to be of ints:
result1 = [int(item) for item in num1.split()]
result2 = [int(item) for item in num2.split()]
Upvotes: 0
Reputation: 6874
n1 = '1234 456' #note the single quotes
n2 = '567 879'
def foo(num):
return num.split()
foo(n1)
foo(n2)
And the output:
['1234', '456']
['567', '789']
Upvotes: 0
Reputation: 17869
Make them strings, so you can use split, and then turn them into integers!!
num1 = '12345'
num2 = '34266 73628'
def func(number):
num = number.split()
return [int(i) for i in num]
>>> func(num1)
[12345]
>>>
>>> func(num2)
[34266, 73628]
Upvotes: 0
Reputation: 129587
I'm going to assume that num1
and num2
are both strings (otherwise you have a syntax error). You can use str.split()
:
>>> num1 = '12345'
>>> num2 = '34266 73628'
>>> num1.split()
['12345']
>>> num2.split()
['34266', '73628']
Upvotes: 3