Hello there,

Currently doing some work for my coursework. I've noticed the "\n" is obviously the new line function or whatever you guys call it, but would you understand what I mean when I say it creates too much of a new line?

For example, I want the newline to do this:

*****
*****


Instead, in my coding it's producing this:

*****

*****

If I'm missing something I'll be very grateful if somebody could point it out! Thank you!

Use CODE tags and post your code.

Notice that print allways adds newline at end if you do not suppress it.

The print function by itself adds a newline at the end. I suspect you are doing something like this (though you showed no code, so I am guessing)

asterisks = '*'*5
print(asterisks+'\n')
print(asterisks+'\n')

This will work just fine without the explicit newline.

Of course if you want an embedded newline, you have to be explicit: print(asterisks+'\n'+asterisks) And if you are reading lines from a text file, they arrive with their newlines intact, so you need to bear that in mind.

Right okay I'll show you what I've got, many thanks for the replies though!

def drawBlocks(a,b,c,d,height):
    a = a * "  "
    b = b * " *"
    c = c * "  "
    d = d * " *"
    print((a + b + c + d + "\n") * height) 

def drawLetterA():
    stars = drawBlocks(1,8,1,0,2)
    stars = drawBlocks(0,2,6,2,2)
    stars = drawBlocks(0,10,0,0,2)
    stars = drawBlocks(0,2,6,2,3)

lines 9 until 12 make it look like functions return something usefull, but they return only None.

You're right, it's line 6 that I think is the issue... However if you run the code you'll notice my problem only occurs after each line of code (from 9-11), and I haven't used the print function? So I just assumed there was a different function from "\n" that would reduce the extra newline issue?

from __future__ import print_function

def drawBlocks(a,b,c,d,height):
    print((a * "  " + b * " *" + c * "  " + d * " *" + "\n") * height, end='') 

def drawLetterA():
    drawBlocks(1,8,1,0,2)
    drawBlocks(0,2,6,2,2)
    drawBlocks(0,10,0,0,2)
    drawBlocks(0,2,6,2,3)

drawLetterA()

Output:

* * * * * * * *  
   * * * * * * * *  
 * *             * *
 * *             * *
 * * * * * * * * * *
 * * * * * * * * * *
 * *             * *
 * *             * *
 * *             * *
from __future__ import print_function

def drawBlocks(a,b,c,d,height):
    print((a * "  " + b * " *" + c * "  " + d * " *" + "\n") * height, end='') 

def drawLetterA():
    drawBlocks(1,8,1,0,2)
    drawBlocks(0,2,6,2,2)
    drawBlocks(0,10,0,0,2)
    drawBlocks(0,2,6,2,3)

drawLetterA()

Why don't you post your output with your code ? It makes better posts. Here we must guess or run your code to see what it does.

@Gribouillis - Sorry bud, you're right I should of. This is what I got:

* * * * * * * *  
   * * * * * * * *  

 * *             * *
 * *             * *

 * * * * * * * * * *
 * * * * * * * * * *

 * *             * *
 * *             * *
 * *             * *

Thanks to pyTony, I now have this:

* * * * * * * *  
   * * * * * * * *  
 * *             * *
 * *             * *
 * * * * * * * * * *
 * * * * * * * * * *
 * *             * *
 * *             * *
 * *             * *

So just to clarify, before I make this post as solved, the

end=''

suppresses any extra new line generated by the end of code or in the case, print function?

Yes, it's the syntax for py3k's print() function. In python 2, without the import, add a , at the end of the print statement to avoid the newline.

>>> if True:
...  print "hello",
...  print "world"
... 
hello world

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.