Daniel Lynes
Daniel Lynes

Reputation: 67

Python, for loop that prints 16 charracters at a time

I am trying to loop through a string and print out every sixteen characters. The string also ends with a semicolon. This is what I have currently but I am looking for something that prints like this ARI:03,21-04,19; This is instead what I am getting

i = 0
for word in SIGNS:
    print(word[:i], end='')
    i += 16

The output is

R
I
:
0
3
,
2
1
-
0
4 
,
1
9
;

Upvotes: 0

Views: 80

Answers (2)

iBug
iBug

Reputation: 37297

You should print 16 characters at a time:

i = 0
#for word in SIGNS:
for i in range(0, len(SIGNS), 16):
    print(SIGNS[i:i+16])

Don't use for word in SIGNS: as it iterates through the string in SIGNS character-by-character, which is what messed you up.

Upvotes: 4

AndreyF
AndreyF

Reputation: 1838

Is there is a chance you want to split your data? It looks like the format of your data is separated by ;. In that case you can use:

data_as_list =  SIGNS.split(';')
for record in data_as_list:
   print(record)

Upvotes: 1

Related Questions