用Python输出月份
时间: 2023-11-05 13:12:27 浏览: 38
以下是Python中输出月份的示例代码:
```python
import datetime
month = datetime.datetime.now().month
print("当前月份是:", month)
```
输出结果为:
```
当前月份是: 10
```
其中,`datetime.datetime.now()`可以获取当前的日期和时间,而`month`属性则表示当前月份。可以根据需要对输出结果进行格式化处理。
相关问题
Python输出月份缩写
Python中可以使用datetime模块来输出月份的缩写。具体的代码如下:
```python
import datetime
# 获取当前日期
now = datetime.datetime.now()
# 输出当前月份的缩写
month_abbr = now.strftime("%b")
print(month_abbr)
```
上述代码中,我们首先导入了datetime模块,然后使用`datetime.datetime.now()`获取当前日期。接着,我们使用`strftime()`方法将当前日期格式化为月份的缩写,其中`"%b"`表示月份的缩写形式。最后,使用`print()`函数输出月份的缩写。
python输入月份输出天数
以下是一个Python程序,可以根据输入的月份输出对应的天数:
```python
month = int(input("请输入月份(1-12):"))
if month in [1, 3, 5, 7, 8, 10, 12]:
print("本月有31天")
elif month in [4, 6, 9, 11]:
print("本月有30天")
elif month == 2:
year = int(input("请输入年份:"))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print("本月有29天")
else:
print("本月有28天")
else:
print("请输入正确的月份(1-12)!")
```
首先,程序通过 `input` 函数获取用户输入的月份。然后,通过 `if-elif-else` 语句判断该月份的天数。如果月份在 [1, 3, 5, 7, 8, 10, 12] 中,则输出 31 天;如果月份在 [4, 6, 9, 11] 中,则输出 30 天;如果月份是 2 月,则需要再次输入年份,并根据闰年的规则判断天数。最后,如果用户输入的月份不在 1-12 的范围内,则输出提示信息。