How to print without line break in Python

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 place them inside an array of integers.

So long so good.

But at the time of printing, instead of printing as an array (without a line break), for each of the 441 figures, prints a new line.

How do I not break line and print a frame with dimensions of 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

    
asked by anonymous 06.06.2017 / 06:15

3 answers

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)

View the result on this link.

    
06.06.2017 / 15:56
3

Just add end='' and that's it!

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

end it defaults to "\n" which is the line break at the end of print, putting '' it does not break line.

See: link

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)

Output:

000000000000000000000
110111011111011111110
010101000100010100010
010101111111010101110
010100000101000101000
011101111101110101110
000001010100010100010
011111010111011101010
000000010001000001010
011111010111011111010
010100010000010101000
010111110111110101110
010000000100000000010
010111110101110111110
010101010101010100000
010101011101010111010
010101000001010001010
010101011111011111010
010001010000000000010
011111011111111111111
    
06.06.2017 / 06:30
0

Antony Gabriel, the way you did not really broke the line, but every 21 I wanted to break. So I made a complement:

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 set the 'break' counter to be able to do this line break every 21, and now there is a 21x21 matrix. Many thanks!

    
06.06.2017 / 15:56