日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python语言print函数_Python 的 print 函数

發布時間:2023/12/13 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python语言print函数_Python 的 print 函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python?2.x 系列已經停止維護了, python? 3.x 系列正在成為主流,盡管有些項目還是python2.x 的,之后寫Python?代碼為了保持兼容性,還是盡量和Python?3 標準保持一致

作為一個Python?newbee 而言, python 2.x 和 3.x 的 最大的區別就是 print 從一個命令 變成了一個函數, raw_input() 被 input() 取而代之

Python?最好的地方 之一就是文檔很齊全,https://docs.python.org/3/? 學習 python的好去處

下方是 從命令行中 使用 help(print) 獲取到的print() 函數的幫助

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.

Optional keyword arguments:

file: a file-like object (stream); defaults to the current sys.stdout.

sep: string inserted between values, default a space.

end: string appended after the last value, default a newline.

flush: whether to forcibly flush the stream.

1. print 函數的格式化輸出

1.1 符號占位符

print("{name} is {age} years old.".format(name = "jack", age = 23)

1.2 類似于C語言的占位符

print("%s is %d years old." % ("jack", 23))

2. print 函數重定向標準輸出到文件

def write_log(string, file_name):

try:

with open(file_name, 'a') as log_file:

print(string, file = log_file)

log_file.close()

except OSError as exc:

tb = sys.exc_info()[-1]

lineno = tb.tb_lineno

filename = tb.tb_frame.f_code.co_filename

print('{} at {} line {}.'.format(exc.strerror, filename, lineno))

sys.exit(exc.errno)

def main():

write_log("hello log!", "journal.txt")

if __name__ == '__main__':

main()

3. print 打印當前系統時間精確到毫秒, 需要導入時間包, 下方的代碼參考 stackflow.com 的作答

importdatetimeprint('Timestamp: {%Y-%m-%d %H:%M:%S:%f}'.format(datetime.datetime.now()))

總結

以上是生活随笔為你收集整理的python语言print函数_Python 的 print 函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。