0

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)
6
  • There's no "column" type in the string datatype, unless you're dealing with matrices or dataframes. Commented Jan 27, 2023 at 13:29
  • print('\n'.join(var[::5]))? Or even print(*var[::5], sep='\n'). For all columns - for i in range(5): print(*var[i::5], sep='\n') Commented Jan 27, 2023 at 13:29
  • Do you mean to print a b c a b c a b c ... in a vertical line? Commented Jan 27, 2023 at 13:34
  • What will be the final output, and how does that differ from what you already have? Commented Jan 27, 2023 at 13:35
  • @lemon the term "column" has a meaning in text layout. Since this is about formatting text and not matrices, I'd assume this meaning. Commented Jan 27, 2023 at 13:35

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

And what output does it produce? Please tell us.
the exact output the question asker has requested in their second code block
Jacob's code worked for the first 4 columns, but it prints the wrong characters for the last column. In this case it printed only "a c"
well spotted, I have updated slightly, should work now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.