Python 使用 format 方法格式化输出学生信息
students = []
for _ in range(3):
name = input('请输入学生姓名:')
number = input('请输入学生学号:')
students.append((name, number))
for student in students:
print('{}同学的学号是{}。'.format(*student))
这个方法使用了'format'方法来格式化输出结果。'format(*student)'中的'*student'表示将'student'元组中的每个元素作为参数传递给'format'方法,从而实现对姓名和学号的替换。结果与之前两种方法相同。
原文地址: https://www.cveoy.top/t/topic/rwv 著作权归作者所有。请勿转载和采集!