python第三章習(xí)題
p115
3.13
s = "Python String"
print(s.upper())
print(s.lower())
print(s.find('i'))
print(s.replace('ing', 'gni'))
print(s.split(' '))
3.14
print("{:>15s}:{:<8.2f}".format("Length", 23.87501))
3.15
num = 389
print(f"二進(jìn)制: {bin(num)}")
print(f"八進(jìn)制: {oct(num)}")
print(f"十進(jìn)制: {num}")
print(f"十六進(jìn)制: {hex(num)}")
print(f"Unicode字符: {chr(num)}")
p124
3.19
import time
獲取當(dāng)前時(shí)間的時(shí)間戳
current_time = time.time()
將時(shí)間戳轉(zhuǎn)換為時(shí)間元組
time_tuple = time.localtime(current_time)
按照指定格式將時(shí)間元組格式化為字符串
formatted_time = time.strftime("%Y-%m-%d", time_tuple)
print(formatted_time)
3.20
import time
current_time = time.time()
time_tuple = time.localtime(current_time)
格式1:年-月-日 時(shí):分:秒
format1 = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
格式2:月/日/年 時(shí):分:秒
format2 = time.strftime("%m/%d/%Y %H:%M:%S", time_tuple)
格式3:星期 月 日 年 時(shí):分:秒
format3 = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
格式4:%Y年%m月%d日
format4 = time.strftime("%Y年%m月%d日", time_tuple)
格式5:%H時(shí)%M分%S秒
format5 = time.strftime("%H時(shí)%M分%S秒", time_tuple)
print(format1)
print(format2)
print(format3)
print(format4)
print(format5)
3.21
import time
start_time = time.time() # 記錄開始時(shí)間
for i in range(101):
print(f"\r進(jìn)度: {i}%", end="")
time.sleep(0.1) # 模擬任務(wù)執(zhí)行耗時(shí)
end_time = time.time() # 記錄結(jié)束時(shí)間
total_time = end_time - start_time
print(f"\n程序運(yùn)行總時(shí)長: {total_time:.2f} 秒")

浙公網(wǎng)安備 33010602011771號(hào)