python, python格式函數(shù)的用法是什么?不知道小伙伴們今天來(lái)看看邊肖的分享吧!
python格式函數(shù)用法詳解:
1.位置
Print (Hello {0}, I am {1} . format (World, python)) # Fill in according to the position subscript.
Print (Hello {}, I'm {) . Format (World, python)) # Automatically fill in according to the order.
Print (Hello {0}, this is {1}. {1} is a new language . format (World, python)) # The same parameter can be filled in multiple times.
輸出:
hello world, this is python.
hello world, this is python.
hello world, this is python. python is a new language.
2. Keys
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.
注意:
訪問(wèn)不帶引號(hào)的字典鍵。
5.類別屬性
class Names():
obj=world
name=python
print(hello {names.obj}, this is {names.name}.format(names=Names))
輸出:
hello world, this is python.
6.神奇參數(shù)
args=[, inx]
kwargs={obj: world, name: python}
print(hello {obj}{} this is {name}.format(*args, **kwargs))
輸出:
hello world, this is python.
注意:
這里的格式(*args,**kwargs)等價(jià)于格式(,inx,obj=world,name=python)。
第二,數(shù)字格式化
第三,其他用法
1.逃跑
print({{hello}} {{{0}}}.format(world))
輸出:
{hello} {world}
2.格式化為函數(shù)變量。
name=python
hello=hello, welcome to {} world!format
print(hello(name))
輸出:
hello, welcome to python world!
3.格式化數(shù)據(jù)時(shí)間
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
python,以上就是本文為您收集整理的python最新內(nèi)容,希望能幫到您!更多相關(guān)內(nèi)容歡迎關(guān)注。