编程将输入的学号,姓名,年龄信息格式化打印到屏幕上。其中,学号靠左对齐,占12个字符,;姓名居中对齐,占20个字符,年龄靠右对齐,占10个字符
时间: 2024-05-13 09:21:09 浏览: 13
。请问如何实现?
答案:可以使用字符串格式化语法,具体如下:
```
stu_id = "10001"
name = "张三"
age = 22
# 使用字符串格式化语法
print("{:<12}{:^20}{:>10}".format(stu_id, name, age))
```
输出结果:
```
10001 张三 22
```
相关问题
python怎么让输出的时候对齐学号 姓名 性别 大学语文 高等数学 大学英语 平均分 102 刷卡机 男 89.0 78.0 66.0 77.66666666666667
可以使用字符串的格式化输出功能来实现对齐输出。以下是一个示例代码:
```
# 定义数据
data = {
'学号': '102',
'姓名': '刷卡机',
'性别': '男',
'大学语文': 89.0,
'高等数学': 78.0,
'大学英语': 66.0,
'平均分': 77.66666666666667
}
# 输出表头
print('{:<5}{:<8}{:<5}{:<10}{:<10}{:<10}{:<10}'.format('学号', '姓名', '性别', '大学语文', '高等数学', '大学英语', '平均分'))
# 输出数据
print('{:<5}{:<8}{:<5}{:<10}{:<10}{:<10}{:<10.2f}'.format(data['学号'], data['姓名'], data['性别'], data['大学语文'], data['高等数学'], data['大学英语'], data['平均分']))
```
输出结果如下:
```
学号 姓名 性别 大学语文 高等数学 大学英语 平均分
102 刷卡机 男 89.0 78.0 66.0 77.67
```
其中,`{:<5}` 表示左对齐并占据5个字符的位置,`{:<8}` 表示左对齐并占据8个字符的位置,`{:<10}` 表示左对齐并占据10个字符的位置,`{:<10.2f}` 表示左对齐并占据10个字符的位置,并保留两位小数。可以根据实际需求调整格式化字符串的内容。
使用python将九九乘法表以左对齐的方法输出到以自己学号命名的txt中,例如’20120667.txt’中包含: 1x1=1; 1x2=2; 2x2=4;
以下是代码实现:
```
# 获取学号
student_id = '20120667'
# 创建文件并写入九九乘法表
with open(f"{student_id}.txt", "w") as file:
for i in range(1, 10):
for j in range(1, i+1):
equation = f"{j} x {i} = {i*j}"
file.write(f"{equation:<9}")
file.write("\n")
```
代码解释:
1. 首先获取学号 `student_id`。
2. 使用 `with open()` 语句打开文件,以写入模式打开(如果文件不存在,则会自动创建)。
3. 使用双重循环遍历每个乘法表的元素。
4. 对于每个元素,使用 f-string 格式化字符串,使用 `<` 符号指定左对齐,并使用 `:<9` 指定字段宽度为 9(由于每个元素占据 8 个字符空间,加上一个空格,需要设置 9 个字符空间)。
5. 在每行结束时,使用 `\n` 换行符。
输出结果:
输出到 `20120667.txt` 文件中,文件内容如下:
```
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
```
注意:输出结果中,每行末尾都会有一个空格。这是因为在格式化字符串中,每个元素之间要求包含一个空格。如果不需要空格,可以将 `f"{equation:<9}"` 改为 `f"{equation:<8}"`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)