设为首页 加入收藏

TOP

python的学习之路day5(三)
2017-12-24 06:07:06 】 浏览:934
Tags:python 习之 day5
后格式化
# c,将10进制整数自动转换为其对应的unicode字符 # d,十进制整数 # o,将10进制整数自动转换成8进制表示然后格式化; # x,将10进制整数自动转换成16进制表示然后格式化(小写x) # X,将10进制整数自动转换成16进制表示然后格式化(大写X) # 传入“ 浮点型或小数类型 ”的参数 # e, 转换为科学计数法(小写e)表示,然后格式化; # E, 转换为科学计数法(大写E)表示,然后格式化; # f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化; # F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化; # g, 自动在e和f中切换 # G, 自动在E和F中切换 # % ,显示百分比(默认显示小数点后6位)
 
 
# 两个最基本的格式化
string = "asdfasgsdf__{0}__asdf__{0}__adhkg__{1}__".format(123, "smelond")
print(string)  # asdfasgsdf__123__asdf__123__adhkg__smelond__
string = "___{name:s}___{age:d}___{name:s}".format(name="smelond", age=16)
print(string)  # ___smelond___16___smelond

# 一些常用的用法
#   空白处填充*,^字符居中,20个空格,s:字符串形式{:*^20s}
#   +有符号数值,d:十进制{:+d}
#   x 直接转换为16进制
#   #如果后面输入的为x,那就转换为16进制后再前面加上0x{:#x}
string = "_____{:*^20s}_____{:+d}_____{:x}_____{:#x}".format("smelond", 123, 15, 15)
print(string)  # _____******smelond*******_____+123_____f_____0xf

string = "百分比:{:%}".format(1.2345)
print(string)  # 百分比:123.450000%
用法
# 一些常用的用法
tpl = "i am {},age {},{}".format("seven", 16, "smelond")
print(tpl)  # i am seven,age 16,smelond

tpl = "i am {},age {},{}".format(*["seven", 16, "smelond"])
print(tpl)  # i am seven,age 16,smelond,注意,如果是列表要在前面加上*

tpl = "i am {0},age {1}, really {0}".format("seven", 16)
print(tpl)  # i am seven,age 16, really seven

tpl = "i am {0},age {1}, really {0}".format(*["seven", 16])
print(tpl)  # i am seven,age 16, really seven,列表需要加上*

tpl = "i am {name}, age {age}, really {name}".format(name="smelond", age=16)
print(tpl)  # i am smelond, age 16, really smelond

tpl = "i am {name}, age {age}, really {name}".format(**{"name": "smelond", "age": 16})
print(tpl)  # i am smelond, age 16, really smelond,注意,如果是字典需要加上两个*

tpl = "i am {0[0]}, age {0[1]}, really {1[2]}".format([1, 2, 3], [11, 22, 33])
print(tpl)  # i am 1, age 2, really 33,列表里面的列表

tpl = "i am {:s}, age {:d}".format("smelond", 16)
print(tpl)  # i am smelond, age 16

tpl = "i am {name:s}, age {age:d}".format(name="smelond", age=16)
print(tpl)  # i am smelond, age 16

tpl = "i am {name:s}, age {age:d}".format(**{"name": "smelond", "age": 16})
print(tpl)  # i am smelond, age 16,字典需要加上**

tpl = "numbers:{:b},{:o},{:d},{:x},{:X},{:%}".format(15, 15, 15, 15, 15, 15.87612)
print(tpl)  # s:1111,17,15,f,F,1500.000000%

tpl = "numbers:{0:b},{0:o},{0:d},{0:x},{0:X},{0:%}".format(15)
print(tpl)  # s:1111,17,15,f,F,1500.000000%

tpl = "numbers:{num:b},{num:o},{num:d},{num:x},{num:X},{num:%}".format(num=15)
print(tpl)  # s:1111,17,15,f,F,1500.000000%
一些常用的用法

 

生成器的使用(生成器是使用函数创建的):

# 普通函数
# def func():
#     return 123
#
#
# ret = func()  # 执行函数拿结果

# 生成器
def func():
    # print("start")
    print(111)
    yield 1
    print(222)
    yield 2
    print(333)
    yield 3  # 如果在函数里面出现了yield,这个函数就称之为生成器


ret = func()
print(ret)
# for i in ret:
#     print(i)  # 输出
# 循环第一次的时候就进入func函数里面去,进入后拿走yield后面的值,所以输出1,然后循环第二次,
# 第二次进入func函数时,找到上一次结尾的位置,从结尾的位置又开始循环(往下走),所以就是输出2
# 然后第三次
r1 = ret.__next__()  # 进入函数找到yield,获取
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python3(二)简单的输入输出及内.. 下一篇Python2.7.14安装和pip配置安装及..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目