import datetimeclass Formater:@staticmethoddef main():# https://docs.python.org/3/library/string.html#formatstringsFormater._format_int()Formater._format_float()Formater._format_time()@staticmethoddef _format_int():# 格式化,指定位置,或者keywordprint('Number {0}, {1}, and {other}.'.format(111, 222, other=333))# 格式化print("int format - Decimal format({0}): {0:d}".format(2))# 定长填充空格print("int format - Pad with empty({0:d}): {0:4d}".format(2))# 定长填充0print("int format - Pad with 0({0:d}): {0:04d}".format(2))# 二进制print("int format - Binary format({0:d}): {0:b}".format(10))# 八进制print("int format - Octal format({0:d}): {0:o}".format(10))# 十六进制print("int format - Hex format(lowercase)({0:d}): {0:x}".format(10))# 十六进制print("int format - Hex format(uppercase)({0:d}): {0:X}".format(10))# 本地格式print("int format - Local format: {0:n}".format(123456789))# 科学计数法print("int format - Scientific notation: {0:e}".format(123456789))# 科学计数法print("int format - Scientific notation: {0:E}".format(123456789))@staticmethoddef _format_float():# 格式化print("float format({0}): {0}".format(1234.56789))# 定长填充空格print("float format - Fixed decimal({0}): {0:3.3f}".format(1234.56789))# 定长填充0print("float format - Padding decimal with 0({0}): {0:08.09f}".format(1234.56789))# 科学计数法print("float format - Scientific notation: {0:e}".format(1234.56789))# 科学计数法print("float format - Scientific notation: {0:E}".format(1234.56789))@staticmethoddef _format_time():x = datetime.datetime.now()print(f'datetime format - Weekday, short version: {x.strftime("%a")}')print(f'datetime format - Weekday, full version: {x.strftime("%A")}')print(f'datetime format - Weekday as a number 0-6, 0 is Sunday: {x.strftime("%w")}')print(f'datetime format - Day of month (01-31): {x.strftime("%d")}')print(f'datetime format - Month name, short version: {x.strftime("%b")}')print(f'datetime format - Month name, full version: {x.strftime("%B")}')print(f'datetime format - Month as a number 01-12: {x.strftime("%m")}')print(f'datetime format - Year, short version, without century: {x.strftime("%y")}')print(f'datetime format - Year, full version: {x.strftime("%Y")}')print(f'datetime format - Hour (00-23): {x.strftime("%H")}')print(f'datetime format - Hour (00-12): {x.strftime("%I")}')print(f'datetime format - AM/PM: {x.strftime("%p")}')print(f'datetime format - Minute (00-59): {x.strftime("%M")}')print(f'datetime format - Second (00-59): {x.strftime("%S")}')print(f'datetime format - Microsecond (000000-999999): {x.strftime("%f")}')print(f'datetime format - UTC offset (+0100): {x.strftime("%z")}')print(f'datetime format - Timezone CST: {x.strftime("%Z")}')print(f'datetime format - Day number of year 001-366: {x.strftime("%j")}')print(f'datetime format - Week number of year, Sunday as the first day of week, 00-53: {x.strftime("%U")}')print(f'datetime format - Week number of year, Monday as the first day of week, 00-53: {x.strftime("%W")}')print(f'datetime format - Local version of date and time: {x.strftime("%c")}')print(f'datetime format - Century: {x.strftime("%C")}')print(f'datetime format - Local version of date: {x.strftime("%x")}')print(f'datetime format - Local version of time: {x.strftime("%X")}')print(f'datetime format - A % character: {x.strftime("%%")}')print(f'datetime format - ISO 8601 year: {x.strftime("%G")}')print(f'datetime format - ISO 8601 weekday (1-7): {x.strftime("%u")}')print(f'datetime format - ISO 8601 weeknumber (01-53): {x.strftime("%V")}')if __name__ == "__main__":Formater.main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。