您可以使用一个简单的循环:
def the_function(s, w, h):
for i in range(0, len(s), w):
print(s[i:i+w])
the_function('<.vv.v.v',2,4)
Output:
<.
vv
.v
.v
注意h
并不是真正需要的,因为一旦您知道了字符串长度和所需的长度,高度就固定了。
但是,您可以使用此参数来定义要打印的最大行数:
def the_function(s, w, h):
for n, i in enumerate(range(0, len(s), w)):
if n>=h:
break
print(s[i:i+w])
the_function('<.vv.v.v', 2, 3)
Output:
<.
vv
.v
对于其他函数,您只需要计算起始位置并打印一次。最后一个参数再次没有用:
def another_function(s, n, w, h):
print(s[n*w:(n+1)*w])
another_function('.<.>>>..v', 1, 3, 3)
Output: >>>