编写程序指打印内容位于正中间人体宽度为30字符高度为5行
时间: 2024-05-02 21:18:25 浏览: 28
```
# -*- coding:utf-8 -*-
# 定义需要打印的内容
content = "Hello World!"
# 计算每行需要填充的空格数
spaces = int((30 - len(content)) / 2)
# 打印5行
for i in range(5):
# 打印空格
print(" " * spaces, end="")
# 打印内容
print(content)
# 输出结果
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
```
相关问题
I love python,打印内容位于正中间整体宽度为30个字符高度为五行
下面是代码实现:
```
print('{:^30}\n{:^30}\n{:^30}\n{:^30}\n{:^30}'.format('I love python', 'I love python', 'I love python', 'I love python', 'I love python'))
```
输出结果如下:
```
I love python
I love python
I love python
I love python
I love python
```
编写程序,使打印内容位于正中间,整体宽度为30字符,高度为5行,打印的内容为I love python
# 方法一:使用字符串的居中方法
content = "I love python"
width = 30
height = 5
for i in range(height):
if i == height // 2:
print(content.center(width))
else:
print("".center(width))
# 方法二:手动计算居中位置
content = "I love python"
width = 30
height = 5
center_row = height // 2
for i in range(height):
if i == center_row:
start_col = (width - len(content)) // 2
print(" " * start_col + content)
else:
print(" " * width)