1

I have a variable which give as output numbers taken from a list every loop.

idx = 0
for i in y:
        listbyte = subprocess.check_output('python foo.py ' + path + str(y[idx]), shell=True).rstrip()
        idx += 1
        listnum = listbyte.decode()
        number = (listnum[0])
        print(number)
        u.append(listnum)

If I print number, the output format is:

1
2
3
4
5

If I put end = '' at the end of the print, it works pretty well, outputing: 12345.

The question is: how can I make this output a variable ? I have to use this format for some code ahead in the program but from what I see the end = '' argument is something restricted to print() function.

It should be something like this:

a = number(listnum[0])
print(a)

>>>12345

Thanks

EDIT: using Megabeets answer, the output now is:

0
09
091
0912
09126
091265
0912655
09126554
091265548
2
  • not a duplicate, the answer of that question doesn't apply to my problem.. Commented Oct 16, 2017 at 14:18
  • 1
    Just create a string variable, perhaps accumulating the elements into a list and then using ''.join(). Commented Oct 16, 2017 at 14:18

3 Answers 3

1

You can use a += (listnum[0]) as you can see in the following example:

a = ''
idx = 0
for i in y:
        listbyte = subprocess.check_output('python foo.py ' + path + str(y[idx]), shell=True).rstrip()
        idx += 1
        listnum = listbyte.decode()
        a += (listnum[0])
        u.append(listnum)

And then a will be a string which contains "12345"

>>> print(a)
12345

In case you want a to be an integer at the end, and not a string, you can parse it using int(a)

Sign up to request clarification or add additional context in comments.

7 Comments

edited question after applying your answer. thanks, btw.
Don't print a in every iteration. Print it outside the loop, only after the for.
Printing out of the loop it gives error...From "a" I can't select to print only the last one ?
Notice how I gave up the number parameter. It is not required here. Try to copy and paste my code and then add print (a) after the loop.
If you have line breaks in the numbers use strip. For example "5\n".strip()
|
1

You can either store the numbers in a string and convert that to int or append to a list and join them later on.

Example for a list of numbers from [1,5] :

Using a list,

>>> nums = []
>>> for i in range(1,6): 
        nums.append(i) 

>>> nums
=> [1, 2, 3, 4, 5]
>>> ''.join(str(x) for x in nums)
=> '12345'
>>> int(''.join(str(x) for x in nums))
=> 12345

Using a str

>>> nums = ''
>>> for i in range(1,6): 
        nums+=str(i)

>>> nums
=> '12345'
>>> int(nums)
=> 12345

Or even do it without any conversions mathematically

>>> nums=0
>>> for i in range(1,6): 
        nums=(nums*10)+i 

>>> nums
=> 12345

2 Comments

The fact is that the numbers are coming with line-break, not in the same line and I can't control the way this works. They are coming from an array... Your solutions sounds perfect for any other situation but not for mine, I guess...
@Link , instead of printing number, just use any of the above technique and then later on after the loop, print it. So for example, using list, instead of doing print(number), do nums.append(number) and then after the loop gets over, use the join function as I have shown to bring them together.
0

print 5 % (101 % 3) and (76 // 3) * 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.