0

I want to make a program that will print out the next line after printing first 5 element. Example :

a = ("200","qwdecf",'acfsdvwvg','dfwrvgrwb','fwrfw','fwfw','wefweg53',"1233",'2344','09845')

Output

"200","qwdecf",'acfsdvwvg','dfwrvgrwb','fwrfw'
'fwfw','wefweg53',"1233",'2344','09845'

Most of them I found was using integers, not strings.. So I'm a little stuck. Thanks for helping.

This is my code now:

line = 0
for i in range(len(a)):
    line+=1
    print(a[:6])
tara
  • 31
  • 4
  • Note that your output can't be obtained in any way, as quotes are not part of the strings, so there is no way to get single or double quotes in the output as in your example. Also, what if you have more than 10 items? What exactly did you find that worked with numbers and couldn't work similarly with strings? – Thierry Lathuille Dec 10 '21 at 10:56
  • Does this answer your question? [What is the most "pythonic" way to iterate over a list in chunks?](https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) – Reti43 Dec 10 '21 at 10:57

3 Answers3

0

Maybe do something like this:

a = (0, 2, "3'", True, 2, "c", 'f')
max_word_count = 5
output = ""

for i, j in enumerate(a):
    if i % max_word_count == 0:
        output += "\n"

    output += f"{str(j)} "
CozyCode
  • 484
  • 4
  • 13
0

This works as you specified in the question:

a = ("200","qwdecf",'acfsdvwvg','dfwrvgrwb','fwrfw','fwfw','wefweg53',"1233",'2344','09845')
next_line=1
output=""
for i in range( len(a) ):
    output+='\"'+a[i]+'\"'
    if next_line == 5:
        output+= "\n"
        next_line=0
    else:
        output+=","
    next_line+=1
print(output)

Output:

"200","qwdecf","acfsdvwvg","dfwrvgrwb","fwrfw"
"fwfw","wefweg53","1233","2344","09845"
Pranu Pranav
  • 353
  • 3
  • 8
0

Same answer as CozyCode - However juste note that in

a = ("200","qwdecf",'acfsdvwvg','dfwrvgrwb','fwrfw','fwfw','wefweg53',"1233",'2344','09845')

for key, value in enumerate(a):
    if (key + 1) % 5 == 0:
        print(value)
    else:
        print(value, end =" ") 

enumerate(a) returns an iterable where each of your initial values are paired with their own index. That's why you can use for key, value in ..

alevani
  • 13
  • 4