Print multiple times in the same line - Python

Print multiple times in the same line - Python
0

Here’s something I need help on with Python:
Using while loop(take i as iterations) and string concatenation(+) print a string(i times) in a single line.
The while loop generally prints each iteration of a string in a new line. I need to print multiple iterations of the string in a single line(wrapping) using string concatenation (+ operator)
Please don’t suggest print(str, end=’ ').

One simple workaround would be to do the following:

import sys
printf = sys.stdout.write

Now, you can just use printf() instead of print(). This will not add a new line, hence you can keep printing in the same single line. (Though it’s not recommended)

Note: Don’t use it in the Python interpreter or last line of Jupyter notebook cell.
(Why? Try and figure it out yourself :wink: )

1 Like

Thanks @GokulNC . The problem is still open though. :slightly_smiling_face:

While working with lists or data values of same type in Python 3, one may use the unpacking operator (*). This works with iterables. Try it out for yourself!!