日期格式化输出任务

第1关:空格分隔格式化输出

实验要求

在三行中分别输入当前的年、月、日的整数值,按要求完成输出。

  1. 输出年月日,空格分隔,格式:2020 09 16

测试数据

输入:

1
2
3
>>>2021
>>>04
>>>26

输出:

1
2021 04 26

代码实现

1
2
3
4
year = input()                         # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print(year, month, date)

第2关:多对象的分隔符号格式化输出

实验要求

在三行中分别输入当前的年、月、日的整数值,按要求完成输出。

  1. 输出年-月-日,连字符“-”分隔,格式:2020-09-16
  2. 输出年/月/日,斜线“/”分隔,格式:2020/09/16
  3. 输出月,日,年,逗号“,”分隔,格式:09,16,2020

测试数据

输入:

1
2
3
>>>2021
>>>04
>>>26

输出:

1
2
3
2021-04-26
2021/04/26
04,26,2021

代码实现

1
2
3
4
5
6
year = input()                         # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print(year, month, date, sep="-")
print(year, month, date, sep="/")
print(month, date, year, sep=",")

第3关:format方式格式化输出

实验要求

在三行中分别输入当前的年、月、日的整数值,按要求完成输出。

  1. str.format()格式输出,格式:2020年09月16日

测试数据

输入:

1
2
3
>>>2021
>>>04
>>>26

输出:

1
2021年04月26日

代码实现

1
2
3
4
year = input()                         # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print("{}年{}月{}日".format(year, month, date))

第4关:字符串拼接方式格式化输出

实验要求

在三行中分别输入当前的年、月、日的整数值,按要求完成输出。

  1. 用字符串拼接方法输出,格式:2020年09月16日

测试数据

输入:

1
2
3
>>>2021
>>>04
>>>26

输出:

1
2021年04月26日

代码实现

1
2
3
4
year = input()                         # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print(year + "年" + month + "月" + date + "日")