字符串转换

#-------------字符串格式化------------------、
#test1
info={
    'name':'lanzili',
    'age':20,
    'school':"sysu"
}
print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%info)

name='lanzili'
age=20
school="sysu"
print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%vars())
print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%vars())
"""
def vars(p_object=None): # real signature unknown; restored from __doc__
    
    vars([object]) -> dictionary
    
    Without arguments, equivalent to locals().
    With an argument, equivalent to object.__dict__.
    
    return {}

"""

print("He is name {0[name]} ,{0[age]} years old ,and major in {0[school]}".format(info))#不能丢掉大括号

def foo(x,y,z):
    return x+y+z

from functools import partial
f=partial(foo,2,4)#这一步类似于为参数设定默认值
a=f(3)#等价于foo(2,4,3)
print(a)

def info(name,age,school):
    print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%vars())

#info('lanzili',22,'sysu')
#f2=partial(info,'lan','sysu')#错误写法,不能跳过age这个参数,要按照顺序
#f2=partial(info,age='lan')#也不能使用关键词来定义
#f2(12,'sysu')#info() got multiple values for argument 'age'

#------------------常见转换--------------------
#略去str(),int(),float(),complex(),repr(),format(),tuple(),list(),set(),dict(),frozenset(),set()

#eval  可以对字符串进行求值
a='123+456*23'
print(eval(a))#10611

#chr(int),将整数转化成ascii
print(chr(99))#c

#ord(char),将字符转化为int
print(ord("c"))#99

#hex(int),bin(int),oct(int),分别将整数转化为16进制,2进制和八进制

#list生成器与条件表达式,很骚
lst=[100,2,324,32,423,435,12,34,45]
lst_new=[i if i<100 else 100 for i in lst]#先写条件表达式,然后写迭代式
print  (lst_new)

转载于:https://my.oschina.net/u/3641281/blog/1556865

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值