How to print without line break in Python

Asked

Viewed 2,758 times

1

matriz = []
def create_matriz(a, b):
    for contador in range(a):
        matriz.append( [0] * b )

def print_matriz(txt):
    cont = 0
    for j in range(a):
        for i in range(b):
            if txt[cont] == "0":
                matriz[i][j] = 0
            elif txt[cont] == "1":
                matriz[i][j] = 1
            print txt[cont]
            cont += 1

code = " #Aqui vai uma sequência de 441(21 x 21) algarismos zeros e uns/Não 
coloquei porque da problema"
a = 21
b = 21
create_matriz(a, b)
print_matriz(code)

Next, I have this code to read a string with 441 digits, and put them inside an integer matrix.

So far so good.

But when printing, instead of printing as a matrix (no line break), for each of the 441 digits, print a new line.

How do I not break line and print a picture with dimensions 21 x 21?

ex:

000000000000000000000 110111011111011111110 010101000100010100010 010101111111010101110 010100000101000101000 011101111101110101110 000001010100010100010 011111010111011101010 000000010001000001010 011111010111011111010 010100010000010101000 010111110111110101110 010000000100000000010 010111110101110111110 010101010101010100000 010101011101010111010 010101000001010001010 010101011111011111010 010001010000000000010 011111011111111111111 000000000000000000000

3 answers

3

Just add end='' and ready!

Detail, added the line: from __future__ import print_function to put the parentheses in the print because I couldn’t add the end='' without the parentheses.

the end it has as standard "\n" which is the line break at the end of the print, placing '' he doesn’t break line.

Behold : https://repl.it/I7lY/1

from __future__ import print_function

matriz = []
def create_matriz(a, b):
    for contador in range(a):
        matriz.append( [0] * b )

def print_matriz(txt):
    cont = 0
    for j in range(a):
        for i in range(b):
            if txt[cont] == "0":
                matriz[i][j] = 0
            elif txt[cont] == "1":
                matriz[i][j] = 1
            print (txt[cont],end='')
            cont += 1

code = """
000000000000000000000
110111011111011111110
010101000100010100010
010101111111010101110
010100000101000101000
011101111101110101110
000001010100010100010
011111010111011101010
000000010001000001010
011111010111011111010
010100010000010101000
010111110111110101110
010000000100000000010
010111110101110111110
010101010101010100000
010101011101010111010
010101000001010001010
010101011111011111010
010001010000000000010
011111011111111111111
000000000000000000000"""
a = 21
b = 21
create_matriz(a, b)
print_matriz(code)

Exit:

000000000000000000000
110111011111011111110
010101000100010100010
010101111111010101110
010100000101000101000
011101111101110101110
000001010100010100010
011111010111011101010
000000010001000001010
011111010111011111010
010100010000010101000
010111110111110101110
010000000100000000010
010111110101110111110
010101010101010100000
010101011101010111010
010101000001010001010
010101011111011111010
010001010000000000010
011111011111111111111
  • This is the right way. Even better if the program is made in Python 3 - the latest version of Python 2. - the 2.7, was made in 2010 - and none of the language enhancements has since been incorporated into Python 2.

1


If I understood the question, one possible solution would be this:

# Criando a string com 441 zeros
zeros = 441*'0'

# Variável para a divisão da string
split = 21

# Criando a "matriz" a partir da string
matrix = [zeros[i:i+split] for i in range(0, len(zeros), split)]

# Imprimindo o resultado
print (matrix)

See the result on this link.

0

Antony Gabriel, the way you went didn’t really break the line, but every 21 I wanted to break. So I did an add-on:

def print_matriz(txt): cont = 0 quebra = 0 for j in range(a): for i in range(b): if txt[cont] == "0": matriz[i][j] = 0 elif txt[cont] == "1": matriz[i][j] = 1 if quebra < 20: print (txt[cont],end='') else: print (txt[cont]) cont += 1 quebra += 1 quebra = 0

I put the break counter so I could do this line break every 21, and now it’s a 21x21 matrix. Thank you very much!

  • My solution became smaller, more legible, more pythonic and probably much faster. :-)

  • And more flexible, since you can vary the 21. ehehehehe!

  • I’m still gonna learn all that. I started to learn Pascal this semester, but as I had already learned a little logic a few years ago I decided to learn a new language in parallel. Ai chose python, and over time I’ll learn these new ideas haha

  • I just recicled your algorithm, the line break occurred in the " n" string at the end of the variable code, but okay, I already knew that somehow I didn’t correctly interpret the problem hehe, sorry.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.