国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

python format函數

來源:懂視網 責編:李贏贏 時間:2021-11-30 10:49:23
文檔

python format函數

python format函數用法詳解:1、位置:根據位置下標進行填充;2、key:obj=world;3、列表:[“world”,python”];4、字典:訪問字典的 key,不用引號;5、類屬性。
推薦度:
導讀python format函數用法詳解:1、位置:根據位置下標進行填充;2、key:obj=world;3、列表:[“world”,python”];4、字典:訪問字典的 key,不用引號;5、類屬性。

python format函數的用法是什么呢?不知道的小伙伴來看看小編今天的分享吧!

python format函數用法詳解:

1、位置

print("hello {0}, this is {1}.".format("world", "python"))  # 根據位置下標進行填充

print("hello {}, this is {}.".format("world", "python"))  # 根據順序自動填充

print("hello {0}, this is {1}. {1} is a new language.".format("world", "python"))  # 同一參數可以填充多次

輸出:

hello world, this is python.

hello world, this is python.

hello world, this is python. python is a new language.

2、key

obj = "world"

name = "python"

print("hello {obj}, this is {name}.".format(obj = obj, name = name))

輸出:

hello world, this is python.

3、列表

list = ["world", "python"]

print("hello {names[0]}, this is {names[1]}.".format(names = list))

輸出:

hello world, this is python.

4、字典

dict = {"obj":"world", "name":"python"}

print("hello {names[obj]}, this is {names[name]}.".format(names = dict))

輸出:

hello world, this is python.

注意:

訪問字典的 key,不用引號。

5、類屬性

class Names():

    obj = "world"

    name = "python"

print("hello {names.obj}, this is {names.name}.".format(names = Names))

輸出:

hello world, this is python.

6、魔法參數

args = [",", "inx"]

kwargs = {"obj": "world", "name": "python"}

print("hello {obj}{} this is {name}.".format(*args, **kwargs))

輸出:

hello world, this is python.

注意:

這里的 format(*args, **kwargs) 等價于 format(",", "inx", obj = "world", name = "python")。

二、數字格式化

三、其他用法

1、轉義

print("{{hello}} {{{0}}}".format("world"))

輸出:

{hello} {world}

2、format 作為函數變量

name = "python"

hello = "hello, welcome to {} world!".format

print(hello(name))

輸出:

hello, welcome to python world!

3、格式化 datatime

from datetime import datetime

now = datetime.now()

print("{:%Y-%m-%d %X}".format(now))

輸出:

2020-12-15 19:46:24

4、{}內嵌{}

print("hello {0:>{1}} ".format("world", 10))

輸出:

hello      world

以上就是小編今天的分享了,希望可以幫助到大家。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

python format函數

python format函數用法詳解:1、位置:根據位置下標進行填充;2、key:obj=world;3、列表:[“world”,python”];4、字典:訪問字典的 key,不用引號;5、類屬性。
推薦度:
標簽: python format
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 黄色毛片在线观看 | 中文字幕不卡在线 | 国产精品高清一区二区 | 国产成人精品999在线观看 | 国产无卡一级毛片aaa | 精品一区二区三区免费毛片爱 | 国产欧美综合在线观看第七页 | 国产成人a∨麻豆精品 | 日韩一区二区视频 | 欧美嫩交| 成人在线一区二区三区 | 亚洲一区日韩一区欧美一区a | 日韩欧美在线第一页 | 日韩区在线 | 久草中文视频 | 丰满老熟好大bbbxxx | 国产伦精品一区二区三区 | 精品视频免费看 | 国产一二三区在线 | 国产成人精品综合久久久 | 色视频久久 | 与子乱刺激对白在线播放 | 91原创在线| 一级网站在线观看 | 国产亚洲欧美日韩俺去了 | 国内一级野外a一级毛片 | 国产精品 视频一区 二区三区 | 在线观看香蕉视频 | 一区二区日韩 | 国产精品久久久久久永久牛牛 | 国产日韩欧美视频在线 | 精品一区二区视频 | 久久久91精品国产一区二区 | 国产精品123区 | 欧美综合在线观看 | 久久久久亚洲精品美女 | 久久亚洲不卡一区二区 | 欧美日韩精| 999国产精品999久久久久久 | 欧美一区二区三区视频在线 | 亚洲精品毛片久久久久久久 |