printingpython-3.12

Python Print() Function Problem - Repeating a Printed Value on the same line


It's my first time on stack so if you notice that i'm not following a certain etiquette please point it out. I do not get upset, do not worry about my feelings. Not new to I.T. or programming but python was a long time ago for me and i'm getting back into it.

Obligatory intro's aside

PROBLEM : I understand how the print() function works, (at least i think i do).

I'm trying (as an exercise in learning) to print a value that repeats on the same line, but it's just not working.

TROUBLESHOOTING : The following i understand...

print("HELLO") this is text string that results in

HELLO

Next... I understand that i can implement math functions

print(5x4) this is a variable that results in

20

Now back to the text string, I understand i can repeat the text string as follows

print("HELLO"*5) and this will result in

HELLOHELLOHELLOHELLOHELLO

and if i wish to put them individually 1 line per hello i can use

print("HELLO"*5\n)

HELLO HELLO HELLO HELLO HELLO

THIS HOWEVER DOESN'T NOT WORK

print(5x4 * 5) this results in 100 Because 5x4x5 =100 obviously

I would like it to output as follows

20 20 20 20 20

so print(calulation then duplicate that answer by the amount of times i specify)

and i do realize i can do this

for i in range(5):
 print(20)

But i'm specifically looking to use the *5 instruction.

THE QUESTION : Is it possible to use the format print(5x4) with the *5 (Multiplier) in a one line statement to produce my desired outcome

I have tried

print(5x4*5)

print(5x4)*5

print[(5x4)*5]

and none of these seem to work, I've been racking my brain with this for approx 2 hours now. I Understand that "print()" is used to print a TEXT STRING, but then.. it isn't always because print(5x4) is not a text string but an int, so i assumed it would be possible to just pop on *5 at the end and it should work.

Any ideas ?

I realize this is basics, i have searched the forum for the answer already, everything relates to either basic print("") Text strings or for loops (which i already understand), but i cannot find an answer to this specific issue (i'm suspecting it may not be able to be done, but if so, i don't understand why ) Thanks ahead of time


Solution

  • Is that what you want?

    print(str(5*4) * 5)