Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print patterns in one line in Python 3?

I have this code to print ‘H’ in a pattern using ‘*’:

def grid_h():
    result_str='';
    for row in range(5):
        for column in range(5):
            if((column == 0 or column == 4)
            or (row == 2)):
               result_str = result_str + '*'
            else:
               result_str = result_str + ' '
        result_str = result_str + '\n'
    return result_str

I’m trying to print HH in the same line but it always go to the new line.

I know there is an end=” at Python 3 to stay in the same line but I have tried this and still not working:
print(grid_h(), end=”), print(grid_h())

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just as @tripleee has mentioned in the comment, I think you should read the guidelines on how to ask questions.

anyway, just to give you a hint, your function returns this unformatted string: '* *\n* *\n*****\n* *\n* *\n'

Doesn’t matter whether you have the end='' flag in print() or not, the newline is included in that string that your grid_h() function returns.

If you want the H logo thingy to be side by side, then you need to figure out how to fix where the \n character is placed…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading