yield生成器及字符串的格式化
一、生成器
def ran():
print('Hello world')
yield 'F1' print('Hey there!')
yield 'F2' print('goodbye')
yield 'F3' ret = ran() # ran()称为生成器函数,ret才是生成器,仅仅具有一种生成能力,函数内部要有关键字yield
print(ret) res = ret.__next__() #对生成器进行循环操作,遇到yield会停止操作,将yield的值返回给变量,并会记录保存位置
print(res) res1 = ret.__next__() #下次再对生成器进行操作,会从停止出开始,直到下一个yield停止
print(res1) # 当__next__次数超过yield时,会报错 for i in ret: #进行__next__之后再进行for循环,也是从上次yield停止处开始
print(i)
二、字符串的格式化
① % 方法
s = 'I am a %s guy' % ('good')
print(s)
n = 'I am a %s guy,%d years old' % ('good',28)
print(n)
d = 'I am a %(n1)s guy,%(n2)d years old' % {'n1':"good",'n2':28}
print(d)
f = 'I am %f' % (28) # 浮点数占位符,默认保留小数点后6位,四舍五入
print(f)
f1 = 'I am %.2f' % (28) #设置保留小数点后2位
print(f1)
# typecode
%s : 字符串
%d : 十进制数字
%f :浮点型
%% :%
%o : 将十进制转换成八进制返回
%x :将十进制转换成十六进制返回
%e :将数字转换成科学记数法
② format方法
tem = 'I am {},age {},'.format('Ethan',28)
print(tem)
tem = 'I am {},age {},{}'.format(*['Ethan',28,'Ethan'])
print(tem)
tem = 'I am {0},age {1},really {0}'.format('Ethan',28)
print(tem)
tem = 'I am {0},age {1},really {0}'.format(*['Ethan',28])
print(tem)
tem = 'I am {name},age {age},really {name}'.format(**{'name':'Ethan',"age":28})
print(tem)
tem = 'I am {name},age {age},really {name}'.format(name = 'Ethan',age = 28)
print(tem)
tem = 'I am {0[0]},age {0[1]},really {0[0]}'.format(['Ethan',28],['Seven',27])
print(tem)
tem = 'I am {:s},age {:d},money {:f}'.format('Ethan',28,8988.23)
print(tem) # I am Ethan,age 28,money 8988.230000
tem = 'I am {:s},age {:d}'.format(*['Ethan',28])
print(tem)
tem = 'I am {name:s},age {age:d}'.format(age = 28,name = 'Ethan')
print(tem)
tem = 'I am {name:s},age {age:d}'.format(**{'name':'Ethan','age':28})
print(tem)
tem = 'Numbers:{:b},{:o},{:d},{:x},{:X},{:%}'.format(15,15,15,15,15,15.87623,2)
print(tem) # Numbers:1111,17,15,f,F,1587.623000%
yield生成器及字符串的格式化的更多相关文章
- Python爬虫与数据分析之进阶教程:文件操作、lambda表达式、递归、yield生成器
专栏目录: Python爬虫与数据分析之python教学视频.python源码分享,python Python爬虫与数据分析之基础教程:Python的语法.字典.元组.列表 Python爬虫与数据分析 ...
- PHP json字符串,格式化缩进显示
PHP json字符串,格式化显示 /** * 格式化 */ class JsonFormatHelper { /** * json字符串缩进显示 * @param unknown $json * @ ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- 使用指定格式的字符串变量格式化日期字符串,DateAndTime取时间间隔
private void btn_GetTime_Click(object sender, EventArgs e) { lab_time.Text = DateTime.Now.ToString(& ...
- python基础的输入字符串的格式化
name = input("name:") age = input ("age:") job = input ("job") info = ...
- python笔记二(数据类型和变量、编码方式、字符串的编码、字符串的格式化)
一.数据类型 python可以直接处理的数据类型有:整数.浮点数.字符串.布尔值.空值. 整数 浮点数 字符串:双引号内嵌套单引号,可以输出 i'm ok. 也可以用\来实现,\n 换行 \t tab ...
- Python字符串与格式化的一点用法
#python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 1.python于C语言不同的是,python没有字符的概念,所谓的字符就是长度为1的字符串,使用切片或者索引同样可以对字 ...
- c语言之字符串和格式化输入输出
字符串和格式化输入输出 #include<stdio.h> #include<string.h> #define DENSITY 62.4 int main(void) { f ...
- #python str.format 方法被用于字符串的格式化输出。
#python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...
随机推荐
- mysql语句查询练习
1.创建students表mysql> create table ...
- Animator Controller 继承关系
准备知识 对于Animator Controller中蜘蛛网一样的几十条连线,后续如果靠人工维护,那成本将是很大. AnimatorOverrideController组件的文档:https://do ...
- struts2中各个jar包作用
Struts2.3.4 所需的Jar包及介绍 Jar包的分类 jar包名称 jar包版本 jar包 文件名 jar包 的作用 jar包内包含的主要包路径及主要类 依赖的自有jar包名称 依赖的第三方j ...
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Linux 中 SVN 重启关闭
a.启动SVN svnserve -d -r /A/B/svn/ 其中 -d 表示守护进程, -r 表示在后台执行 /A/B/svn/ 为svn的安装目录 b.关闭SVN 这里采取linux杀死进程的 ...
- 修改Linux用户的UID、GID
对于NFS共享文件,保留文件权限,需要UID.GID与nfs-server端一致! 试验环境:Centos6.5_64/172.24.0.26 01.用户的UID和GID不能被占用 [root@26 ...
- 佛祖保佑 永无bug
/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...
- [转]别再抱怨了,国内这么多优秀的Android资源你都知道吗?
因为一些大家都知道的原因,android很多官方出品的优秀开发资源在国内无法访问. 国内的同行们对此也做出了很多努力,有很多朋友通过各种手段把很多优秀的资源搬运到了国内,为国内android开发者提供 ...