Skip to main content
 首页 » 编程设计

Python 3.4-定期插入空格

2025年05月04日1152php

<分区>

我正在尝试让 Python 允许我在字符串中定期(每第 5 个字符)插入一个空格。 这是我的代码:

str1 = "abcdefghijklmnopqrstuvwxyz" 
list1 = [] 
list2 = [] 
count = 3 
space = " " 
 
# converting string to list 
for i in str1: 
    list1.append(i) 
print(list1) 
 
# inserting spaces 
for i in list1: 
    mod = count%6 
    count = count + 1 
    if mod == 0: 
        list1.insert(count,space) 
        count = count + 1 
#converting back to a string 
list2 = "".join(list1) 
print(str(list2)) 

但是它将第一部分组合为 7。 谁能帮我解决这个问题?