一、生成器

 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生成器及字符串的格式化的更多相关文章

  1. Python爬虫与数据分析之进阶教程:文件操作、lambda表达式、递归、yield生成器

    专栏目录: Python爬虫与数据分析之python教学视频.python源码分享,python Python爬虫与数据分析之基础教程:Python的语法.字典.元组.列表 Python爬虫与数据分析 ...

  2. PHP json字符串,格式化缩进显示

    PHP json字符串,格式化显示 /** * 格式化 */ class JsonFormatHelper { /** * json字符串缩进显示 * @param unknown $json * @ ...

  3. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  4. 使用指定格式的字符串变量格式化日期字符串,DateAndTime取时间间隔

    private void btn_GetTime_Click(object sender, EventArgs e) { lab_time.Text = DateTime.Now.ToString(& ...

  5. python基础的输入字符串的格式化

    name = input("name:") age = input ("age:") job = input ("job") info = ...

  6. python笔记二(数据类型和变量、编码方式、字符串的编码、字符串的格式化)

    一.数据类型 python可以直接处理的数据类型有:整数.浮点数.字符串.布尔值.空值. 整数 浮点数 字符串:双引号内嵌套单引号,可以输出 i'm ok. 也可以用\来实现,\n 换行 \t tab ...

  7. Python字符串与格式化的一点用法

    #python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 1.python于C语言不同的是,python没有字符的概念,所谓的字符就是长度为1的字符串,使用切片或者索引同样可以对字 ...

  8. c语言之字符串和格式化输入输出

    字符串和格式化输入输出 #include<stdio.h> #include<string.h> #define DENSITY 62.4 int main(void) { f ...

  9. #python str.format 方法被用于字符串的格式化输出。

    #python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...

随机推荐

  1. 一种面向对象的TCP/IP中间件

    这是一个使用C++封装的TCP/IP协议栈(仅传输层),属于本人所设计的中间件的一员,具有硬件无关,应用无关特性,使用非常方便,一看代码便知: #include "net.h" / ...

  2. 项目自动化建构工具gradle 入门0——环境 & 废话

    gradle 是一个项目自动化构建工具.同类的产品还有ant ,maven等等.相比之下我更喜欢gradle,它语法简洁.兼容maven.ide集成很好. 学习使用gradle最快的方式是看文档,而且 ...

  3. polya/burnside 学习

    参考链接: http://www.cnblogs.com/hankers/archive/2012/08/03/2622231.html http://blog.csdn.net/raalghul/a ...

  4. rxjs5.X系列 —— transform系列 api 笔记

    欢迎指导与讨论:) 前言 本文是笔者翻译 RxJS 5.X 官网各类operation操作系列的的第一篇 -- transform转换.如有错漏,希望大家指出提醒O(∩_∩)O.更详细的资料尽在rxj ...

  5. C++函数指针总结

    学习c++的过程中,指针是难点,熟悉了指针之后,还有一个让人很蛋疼的难点,那就是函数指针了.本博文详细介绍一下常见的各种坑爹的函数指针. 至于指针的详细学习,推荐这篇博文C++指针详解 与数据一样,函 ...

  6. ubuntu源笔记

    比如说清华大学的ipv6镜像源:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ /etc/apt/sources.list为包管理工具apt的软件包 ...

  7. [LeetCode] Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  8. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  9. WPF仿百度Echarts人口迁移图

    GitHub地址:https://github.com/ptddqr/wpf-echarts-map/tree/master 关于大名鼎鼎的百度Echarts我就不多说了 不了解的朋友直接看官方的例子 ...

  10. JaveScript-解决表格使用滚动条时冻结表头栏问题

    解决方法: //设置表格表头里的th==表格内容里的td function ThEqualTd(thId, tdId) { var tdNum = document.getElementById(td ...