0

Like in BASH, I can use "echo -n" within a loop and print all elements in an array (or list in Python) in a single line without any space " " character. See the "echo -n value" below in second for loop.

Arun Sangal@pc MINGW64 ~
$ a=(1 2 33 four)

Arun Sangal@pc MINGW64 ~
$ for v in ${a[@]}; do echo $v; done
1
2
33
four

Arun Sangal@pc MINGW64 ~
$ for v in ${a[@]}; do echo -n $v; done
1233four
Arun Sangal@pc MINGW64 ~
$ for v in ${a[@]}; do echo -n "$v "; done
1 2 33 four

How can I do the same in Python using for loop while iterating each list value.

PS: I don't want to use "".join(list). Also, it seems like "".join(list) will WORK only if my list in Python has values as "strings" type then "".join(list) will work (like BASH echo -n) but if the list has Integer aka non-string values (for ex: as shown in the list numbers in the code below), "".join(numbers) or "".join(str(numbers)) still didn't work (like BASH echo -n); in this case, even though I used print num, in the loop, it still introduced a space " " character.

I'm trying to use print value, (with a command) but it's introducing a space " " character.

In Python 2.x, the following code is showing:

numbers  = [7, 9, 12, 54, 99] #"".join(..) will NOT work or print val, will not work here
strings  = ["1", "2", "33", "four"] #"".join(..) will work as values are strings type.

print "This list contains: "

#This will print each value in list numbers but in individual new line per iteration of the loop.
for num in numbers:
    print num

print ""
print "".join(str(numbers))
print "".join(strings)

#PS: This won't work if the values are "non-string" format as I'm still getting a space character (see output below) for each print statement for printing num value
for num in numbers:
    print num,

output as:

This list contains: 
7
9
12
54
99

[7, 9, 12, 54, 99]
1233four
7 9 12 54 99

What I'm looking for is, how can I print all elements of list numbers (as shown below) using for loop and a print statement in a single line when the list has non-string values?

79125499
AKS
  • 16,482
  • 43
  • 166
  • 258
  • Have a look a t this answer: http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space – m13r Aug 05 '16 at 12:28
  • @m13r Hi, If I'm right, none of the examples in the above link is using a list/array with some/all integer values and none addressed the issue that I have which is how to NOT print " " while printing a list's value in a FOR loop. Please correct otherwise. – AKS Aug 05 '16 at 12:36
  • BTW, while it works well enough to get across your intent here, `echo -n` isn't actually good form in shell scripts. See the POSIX spec for `echo` at http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html -- using `-n` makes output entirely implementation-defined; the appropriate way to write content without a newline is `printf '%s' "content"`, or (to process backslash escapes) `printf '%b' "content"`. – Charles Duffy Aug 05 '16 at 12:40

2 Answers2

1

You could to this:

a = [1, 2, 3, 4]

concat = ""

for i in a:
    concat = concat + str(i)

print concat

or without using a separate variable:

# needed if Python version 2.6+
from __future__ import print_function

a = [1, 2, 3, 4]

for i in a:
    print(i, end="")
m13r
  • 2,458
  • 2
  • 29
  • 39
  • 1
    Yes, I agree but isn't it possible in Python directly without requiring to create an extra object (i.e. concat like you advised). – AKS Aug 05 '16 at 12:37
  • 1
    I've added a second code snippet to the solution without an additional variable... – m13r Aug 05 '16 at 12:43
1

If I understand you right, you are trying to print numbers without space between them? If so try the following.

from __future__ import print_function

for i in range(1, 11):
    print (i, end='')

Result

12345678910
Process finished with exit code 0
cyberbemon
  • 3,000
  • 11
  • 37
  • 62
  • OK, that did the trick. Thanks. It's strange that now I have to live with 2.x 3.x versions and compatibilities. In 3.x it'd be good, right? – AKS Aug 05 '16 at 12:46