1 Star 0 Fork 0

ecity/my-python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
format.py 4.21 KB
一键复制 编辑 原始数据 按行查看 历史
绿巨人 提交于 2023-02-13 00:07 +08:00 . init
import datetime
class Formater:
@staticmethod
def main():
# https://docs.python.org/3/library/string.html#formatstrings
Formater._format_int()
Formater._format_float()
Formater._format_time()
@staticmethod
def _format_int():
# 格式化,指定位置,或者keyword
print('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))
# 定长填充0
print("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))
@staticmethod
def _format_float():
# 格式化
print("float format({0}): {0}".format(1234.56789))
# 定长填充空格
print("float format - Fixed decimal({0}): {0:3.3f}".format(1234.56789))
# 定长填充0
print("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))
@staticmethod
def _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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/sn-yang/my-python.git
git@gitee.com:sn-yang/my-python.git
sn-yang
my-python
my-python
master

搜索帮助