一、生成器

 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. 基于TQ2440的SPI驱动学习(OLED)

    平台简介 开发板:TQ2440 (NandFlash:256M  内存:64M) u-boot版本:u-boot-2015.04 内核版本:Linux-3.14 作者:彭东林 邮箱:pengdongl ...

  2. An error occurred during the installation of assembly 'Microsoft.VC90.CRT……的问题

    有一段时间没有用到AnkhSvn了,今天工作需要安装了一下.结果安装到一半就无法继续了,提示An error occurred during the installation of assembly ...

  3. java基础算法之快速排序

    快速排序(Quicksort)是对冒泡排序的一种改进.在大学学过之后现在基本忘了,最近在好多地方都看到说快速排序在面试会问到,于是自己也准备重新拾起以前忘记的东西来,慢慢的积累自己的基础知识.figh ...

  4. TCP/IP协议(一)网络基础知识

    参考书籍为<图解tcp/ip>-第五版.这篇随笔,主要内容还是TCP/IP所必备的基础知识,包括计算机与网络发展的历史及标准化过程(简述).OSI参考模型.网络概念的本质.网络构建的设备等 ...

  5. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. 【WPF】Combobox指定选中值用selectedValue不是很灵的时候,

    wpf combobox 指定选中的值,前题,combobox是通过数据库绑定的ItemsSource:所以再指定的时候用selectValue不是很成功!我的解决方法是 生成一个字典,办值和索引对应 ...

  8. 详解三种缓存过期策略LFU,FIFO,LRU(附带实现代码)

    在学操作系统的时候,就会接触到缓存调度算法,缓存页面调度算法:先分配一定的页面空间,使用页面的时候首先去查询空间是否有该页面的缓存,如果有的话直接拿出来,如果没有的话先查询,如果页面空间没有满的时候, ...

  9. gpu对任意长度的矢量求和

    blockDim.x*gridDim.x 跳过一个grid int <<<参数1,参数2>>>(int *a,int * b,int * c); 如果是一维的,参数 ...

  10. 递推 hdu 3411

    http://blog.csdn.net/wust_xhj/article/details/47779539 怎么推可以看这里 f[0]=0 f[1]=1 [0,1]* | 0  q  |(n-1)= ...