python题目求答案,求解答

将字符串s = "I want to study Python perfectly"转换为元组,并按照5个一行的格式输出

s = "I want to study Python perfectly"
a=tuple(s)
n=1
for i in a:
    print(i,end='')
    if n%5==0:
        print()
    n+=1
s = "I want to study Python perfectly"

e = tuple(s.split())
j = 1
for i in e:
    print(i, end=' ')
    if j % 5 == 0 :
        print("")
    j += 1

代码如下

s = "I want to study Python perfectly"
b=tuple(s) # 转化为元组
for i in range(len(b)):
    print(b[i],end='') #end='',目的是不换行
    if (i+1)%5==0: # 因为i是从0开始,但计数要从1开始,所以要加1
        print(end='\n')

如有帮助,请采纳


s = "I want to study Python perfectly"
t1=tuple(list(s.split(' ')))
print(t1)
for i in range(1,len(t1)+1):
    #print(i, end="")
    if(i%5==0):
        print(t1[i-1])
    else:
        print(t1[i-1]+" ", end="")