Liam87
Liam87

Reputation: 63

Return one string on multiple lines

I am trying to get the function get_histogram to return a string that takes a character and creates a separate line for each integer in a list.

def get_histogram(list1, char):
    output = ""
    for i in list1:
        output = output + (char * i)
    return output

get_histogram([3,1,5], "*")

It return all on one line like this:

'*********'

I need it to get it to return like this:

***
*
*****

It needs to return as one string, I can't just print separate lines.

Upvotes: 0

Views: 536

Answers (2)

YoungCoder5
YoungCoder5

Reputation: 945

#!/usr/bin/python3
__author__ = 'Aidan'


class Do():
    def get_histogram(self, list1, char):
        output = ""
        for i in list1:
            output = output + (char * i) + '\n'
        return output
a = Do()
ab = [1, 2, 3]
re = a.get_histogram(list1=ab, char='*')
print(re)

Sorry, I fixed my error(LOL).

Upvotes: 0

David Robinson
David Robinson

Reputation: 78610

Change your line to

        output = output + (char * i) + "\n"

This works because "\n" represents a newline.

Upvotes: 2

Related Questions