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
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