Newline Suppression in print stmt not working in function

I discovered that a print statement using newline suppression (end=’ ') is in a function the newline suppression does not work. If the same code is placed inline (in main), it works properly. Anyone ever come across this? Using Python 3.12.
Here is some example code:

tprint("\nHex Buffer Dump")

src_list = [128, 192, 64, 228, 32, 244, 16, 0, 8, 156, 4, 15, 53, 23, 12, 255]
data = bytes(src_list)

def dump_buff(buff):
  j = 0;
  for i in range(len(buff)):
    print("{:02X}".format(buff[i]), end=' ')
    j += 1
    if j == 8:
      j = 0
      print()
    print()
  
# test code inline
print("\nCode inline")
j = 0
for i in range(len(data)):
  print("{:02X}".format(data[i]), end=' ')
  j += 1
  if j == 8:
    j = 0
    print()

# test when code in function
print("\nCode in function")
dump_buff(data)ype or paste code here

Here is what output looks like:

Hex Buffer Dump

Code inline (works as expected)
80 C0 40 E4 20 F4 10 00 
08 9C 04 0F 35 17 0C FF 

Code in function (new line suppression not honored)
80 
C0 
40 
E4 
20 
F4 
10 
00 

08 
9C 
04 
0F 
35 
17 
0C 
FF 

The code in your function has a bare print() call in every iteration of the loop (a second one, after the if j == 8 clause is over). You’re effectively saying “print the hex code with end=' '” and then a few lines later telling it “now print me a newline on its own”

If you highlight the output you can see the spaces at the end of each line, before the newline.

1 Like

You are correct. Indentation error on my part. Thanks for taking the time to respond.

You may have noticed that most Python code you see uses 4 spaces for indentation. You seem to be using only 2 spaces. Maybe there’s a lesson to be learned here…