Python: Printing Horizontally Rather Than Current Default Printing

Printing horizontally in loops

@Patrick Artner's answer is correct, but I just wanted to add on a use case that will work in certain IDE's as well as the Windows command prompt.

In order to get the desired animation, you have to also include sys, and use sys.stdout.flush() in each iteration of the loop, like so:

import time
import sys

snort = "SNoooOOO000oooRT"

for char in snort:
sys.stdout.flush()
time.sleep(0.1)
print(char,end='')

If you do not do this in the command prompt (or certain IDE's), the program will output the entire string after it has slept for each iteration of the loop.

You can actually also force print() to flush the output buffer by including the flush parameter in your print statement. So you can avoid importing sys if you do this:

print(char,end='', flush=True)

Can't figure out how to print horizontally in python?

Two options:

Accumulate a result string and print that at the end:

result = ""  
for x in range (1, 21):

if x%15==0:
result = result + "fizzbuzz "

etc...
print result

Or tell Python not to end the printed string with a newline character. In Python 3, which you seem to be using, you do this by setting the end argument of the print function, which is "\n" (a newline) by default:

for x in range (1, 21):  

if x%15==0:
print("fizzbuzz",end=" ")

etc...

Historical note: In Python 2, this could be achieved by adding a comma at the end of the print statement; print "fizzbuzz",

How to print out the list items horizontally?

list1 = ['0', '1', '1', '1', '2', '3', '4', '5', '7', '9']
print("".join(list1))

Python print horizontally on multiple lines at once

One approach could be to build out the ord values using a list comprehension, and then iterate over each char and ord pair in the string, and then print out each char padded to the string length of each ord - i.e. a numeric value of 102 would mean a pad width of 3.

string = "hello world"
ords = [ord(ch) for ch in string]

for ch, ord_ in zip(string, ords):
len_ord = len(str(ord_))
print(ch.ljust(len_ord), end=' ')

print()
print(*ords)

Result:

h   e   l   l   o      w   o   r   l   d   
104 101 108 108 111 32 119 111 114 108 100

If you need to split this off into chunks, for example if your string is too long and you need it to fit your terminal or window, you can use an approach as mentioned here in order to split the string into chunks, and still keep the same logic above.

Here's an example of how that could work. Note that you'll need to adjust chunk_size based on width of your terminal window.

string = "hello world, how is it going there?"
ords = [ord(ch) for ch in string]

# TODO: can set this based on your terminal width
chunk_size = 10

for i in range(0, len(string), chunk_size):
ords_chunk = ords[i:i + chunk_size]
for ch, ord_ in zip(string[i:i + chunk_size], ords_chunk):
len_ord = len(str(ord_))
print(ch.ljust(len_ord), end=' ')
print()
print(*ords_chunk)

Output:

h   e   l   l   o      w   o   r   l   
104 101 108 108 111 32 119 111 114 108
d , h o w i s
100 44 32 104 111 119 32 105 115 32
i t g o i n g t
105 116 32 103 111 105 110 103 32 116
h e r e ?
104 101 114 101 63

How to print the characters horizontally?

Right now you're printing out everything on its own new line by using print. You can make these all into one string by storing the values in a string variable.

import random
def rand_gen1():
output = ""
for i in range(8):
output += random.choice('0123456789abcdefghijklmnopqrstuvwxyz')
print output

Strings Iteration - Printing a String Horizontally

You may use print_function from __future__.


CODE 1

from __future__ import print_function
for word in "Monty Python":
print(word,end=' ')

OUTPUT 1

M o n t y   P y t h o n 

If you don't want space separated OUTPUT, then replace end=' ' with end=''.

CODE 2

from __future__ import print_function
for word in "Monty Python":
print(word,end='')

OUTPUT 2

Monty Python 


Related Topics



Leave a reply



Submit