您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python中文本周围的动态边框

python中文本周围的动态边框

因此,您希望将字符串拆分width字母块,而不是按字母输入。采取公认的答案:

def chunkstring(string, length):
    return (string[0+i:length+i] for i in range(0, len(string), length))

sentence = input('Sentence: ')
width = int(input('Width: '))

print('+-' + '-' * width + '-+')

for line in chunkstring(sentence, width):
    print('| {0:^{1}} |'.format(line, width))

print('+-' + '-'*(width) + '-+')

示例运行:

Sentence: You are only young once, but you can stay immature indefinitely. 
Width: 26
+----------------------------+
| You are only young once, b |
| ut you can stay immature i |
|       ndefinitely.         |
+----------------------------+
python 2022/1/1 18:50:19 有377人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶