•連續輸入字符串,請按長度爲8拆分每一個字符串後輸出到新的字符串數組;
•長度不是8整數倍的字符串請在後面補數字0,空字符串不處理。數組
連續輸入字符串(輸入2次,每一個字符串長度小於100)app
輸出到長度爲8的新字符串數組spa
abc00000
12345678
90000000
解決方案:
使用for循環將大於8位的字符拆分,寫入新的數組,將第9位到剩餘的字符繼續循環調用,最終小於8的字符經過+00000000再截取來補0
方法一:
1 x=input() 2 y=input() 3 z=[] 4 while len(x)>8: 5 x1=x[0:8] 6 z.append(x1) 7 x=x[8:] 8 x=x+'00000000' 9 x=x[0:8] 10 z.append(x) 11 while len(y)>8: 12 y1=y[0:8] 13 z.append(y1) 14 y=y[8:] 15 y=y+'00000000' 16 y=y[0:8] 17 z.append(y) 18 for i in z: 19 print(i)
方法二:(再用一次for循環,減小一半步驟)code
1 x=input() 2 y=input() 3 z=[x,y] 4 r=[] 5 for i in z: 6 while len(i)>8: 7 i1=i[0:8] 8 r.append(i1) 9 i=i[8:] 10 i=i+'00000000' 11 i=i[0:8] 12 r.append(i) 13 for i in r: 14 print(i)