Search code examples
pythonstringtext-manipulation

How to print each column of strings separately in python?


I have used python to print a string "aaaaabbbbbccccc" into 5 separate columns of characters. I have not used an array to do this.

a a a a a
b b b b b 
c c c c c 

Now I want to print each column separately from each other, so for example print only the first column and then do this for all other columns:

a
b
c

To print the characters in 5 different columns I have used:

var="aaaaabbbbbccccc" 
count=0 
for char in var:   
 if count<4:
  print(char, end="  ")
  count=count+1   
 else:
  count=0
  print(char)

Solution

  • Unsure what the aim of this code is, but I have tried to stick to your existing code structure:

    var = 'aaaaabbbbbccccc'
    cnt = 0
    col_to_print = 4 # change this to print a different col.
    for char in var:
        if cnt == col_to_print:
            print(char)
        if cnt<4:
            cnt +=1
        else:
            cnt = 0
    

    output:

    a
    b
    c