python, python格式函数的用法是什么?不知道小伙伴们今天来看看边肖的分享吧!
python格式函数用法详解:
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.
注意:
访问不带引号的字典键。
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.
注意:
这里的格式(*args,**kwargs)等价于格式(,inx,obj=world,name=python)。
第二,数字格式化
第三,其他用法
1.逃跑
print({{hello}} {{{0}}}.format(world))
输出:
{hello} {world}
2.格式化为函数变量。
name=python
hello=hello, welcome to {} world!format
print(hello(name))
输出:
hello, welcome to python world!
3.格式化数据时间
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最新内容,希望能帮到您!更多相关内容欢迎关注。