动态参数

动态参数

1.

def show(*arg):
    print(arg,type(arg))
show(1,22,33,44)
(1, 22, 33, 44) <class 'tuple'>   转化为元组

2.

def show(**arg):
    print(arg,type(arg))
show(n1=1,n2=22,n3=33,n4=44)
{'n1': 1, 'n2': 22, 'n3': 33, 'n4': 44} <class 'dict'>   转化为字典

3.

def show(*args,**kargs):
    print(args,type(args))
    print(kargs, type(kargs))
show(1,22,33,44,n1=55,k2=66)
(1, 22, 33, 44) <class 'tuple'>   转化为元组
{'n1': 55, 'k2': 66} <class 'dict'>  转化为字典

4.

def show(*args,**kargs):
    print(args,type(args))
    print(kargs, type(kargs))
l = [1,22,33,44]
d=  {'n1':55,'k2':66}
show(*l,**d)
(1, 22, 33, 44) <class 'tuple'>   转化为元组
{'n1': 55, 'k2': 66} <class 'dict'>  转化为字典

使用动态参数实现字符串的格式化

1.

s = "{0} is {1}"
result = s.format('dan','beautiful')
print(result)
dan is beautiful

2.

l = ['dan','beautiful']
result = s.format(*l)
print(result)
dan is beautiful

3.

s = "{name} is {acter}"
result = s.format(name='dan',acter='nice')
print(result)
dan is nice

4.

d = {'name':'dan','acter':'nice'}
result = s.format(**d)
print(result)
dan is nice
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值