Shlok Shah
Shlok Shah

Reputation: 23

How to get values from arrays in one single line(Python 3)

I want to get all the values from the array [a,b,c,d,e,f,g] in one line and save it into another variable in Python 3

#This is the code I made and am getting the output "abcdefg" but am not sure 
#how to store the output ,instead of printing it out.
array_value = [a,b,c,d,e,f,g]
for x in array_value:
   print(x, end = '')

It might be a easy problem but I'm new to python and coding in general.

Upvotes: 2

Views: 2280

Answers (2)

heslant
heslant

Reputation: 11

I am not sure, if I really get your problem. The solution depends on your types in array. For example with letters or strings it should be something like this:

array_value = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
output = "";

for item in array_value:
    output += item
    output += ' '

print(output)

If you have in array integers, floats or something else, try parse value with str(variable). But maybe helps context of your problem, like what type of output variable you would like to have. I am just guessing what you are looking for.

Hope it helps.

Upvotes: 1

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39890

You can use str.join(iterable):

Return a string which is the concatenation of the strings in iterable. If there is any Unicode object in iterable, return a Unicode instead. A TypeError will be raised if there are any non-string or non Unicode object values in iterable. The separator between elements is the string providing this method.

The following should do the trick:

array_value = ['a','b','c','d','e','f','g']
output_string = ''.join(array_value)
print(output_string)

>>> "abcdefg"

Upvotes: 4

Related Questions