笨办法学Python - 习题6-7: Strings and Text & More Printing
1、习题 6: 字符串(string) 和文本
学习目标:了解字符串的定义,学会使用复杂的字符串来建立一系列的变量。学会命名有意义的变量名
习题六中的练习代码是:
#! -*-coding=utf-8 -*-
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary,do_not)
print x
print y
print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny ? I %r"
print joke_evaluation % hilarious
w = "This is the left side of ..."
e = "a string with a right side."
print w + e
上述代码的运行结果为:
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo6/Exer6-1.py
There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny ? I False
This is the left side of ...a string with a right side.
Process finished with exit code 0
上面的代码主要是有几个点需要注意下:
- 占位符的问题,%d 代表整数,%s 代表字符串,数据类型必须要匹配
- %r 和 %s 的区别和联系,具体详情请参考习题5
- “+”号带来的字符串拼接的问题,具体详情请也参考习题5
- 格式化输出用到的占位符还有一个重要的问题就是变量和值的一,一对应
2、加分习题:
- 通读程序,在每一行的上面写一行注解,给自己解释一下这一行的作用。
- 找到所有的”字符串包含字符串”的位置。
- 解释一下为什么 w 和 e 用 + 连起来就可以生成一个更长的字符串。
3、我的答案
3.1、通读程序,在每一行的上面写一行注解,给自己解释一下这一行的作用
#! -*-coding=utf-8 -*-
# 定义变量x ,并用占位符表示格式化字符串
x = "There are %d types of people." % 10
# 定义变量 并用格式化输出,变量一,一对应
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary,do_not)
print x
print y
# 格式化打印输出
print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny ? I %r"
print joke_evaluation % hilarious
w = "This is the left side of ..."
e = "a string with a right side."
# 字符串的拼接
print w + e
3.2、找到所有的”字符串包含字符串”的位置
y = "Those who know %s and those who %s." % (binary,do_not)
print "I said: %r." % x
print "I also said: '%s'." % y
3.3、解释一下为什么 w 和 e 用 + 连起来就可以生成一个更长的字符串
因为这里是Python中字符串的功能,其实当使用加号运算符的时候会调用这个类的_ add() _函数,这个函数是每个类都有的,对于自定义的类,不重写这个方法,+这个运算符就没作用.
4、习题总结
习题6主要是介绍了字符串的格式化输出,% 以及 + 的运用,具体还有.format操作上一题也做了详细的阐述和练习,所以还是能看出来字符串的格式化输出还是很重要的。
5、习题 7: 更多打印
学习目标:熟练掌握字符串的格式化输出,这节主要是练习为主。
习题七中的练习代码是:
#! -*-coding=utf-8 -*-
# print的打印输出
print "Mary had a little lamb."
# 字符串的格式化输出
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
# 字符串的 *n 的特殊用法,相当于重复多少次
print "." * 10 ## what'd that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# 字符串的拼接
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12
上述代码的执行结果为:
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo7/Exer7-1.py
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
Process finished with exit code 0
注:上述代码只是对之前几道习题的一个总结,还得课后多练习,毕竟单独靠这几道习题是远远不够的,
有一个知识点,字符串和数字的乘法 - 相当于将字符串重复了多少遍
举个栗子:

还有个需要注意的地方,上面代码的最后两行,最后两个print语句中间有逗号;
# 字符串的拼接
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12
注意:在Python2 中这样是可以执行的,不会报错,就相当于逗号在 print 中的作用是分隔多个待打印的值。如果去掉这个逗号,则相当于两个print 语句,相当于分开打印,会输出两行。
但是在Python3 中是不支持去掉在两个print 语句中间加逗号的,去掉就会报错。

6、习题总结
习题7主要是回顾了之前学习的知识点,包括print操作,字符数串的格式化输出等。
笨办法学Python - 习题6-7: Strings and Text & More Printing的更多相关文章
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学Python - 习题11-12: Asking Questions & Prompting People
目录 1.习题 11: 提问 2.习题 12: 提示别人 3.总结 1.习题 11: 提问 学习目标:了解人机交互场景,熟悉raw_input 的用法. 1.在 Python2.x 中 raw_inp ...
- 笨办法学Python - 习题8-10: Printing & Printing, Printing
目录 1.习题 8: 打印,打印 2.习题 9: 打印,打印,打印 3.习题 10: 那是什么? 3.1.转义序列: 4.习题总结: 1.习题 8: 打印,打印 学习目标:继续学习 %r 的格式化输出 ...
- 笨办法学Python - 习题5: More Variables and Printing
1.习题 5: 更多的变量和打印 学习目标:了解用户输入方法,明白pthon2和Python3之间的用户输入的区别.了解格式化字符串(format string)的概念,学会如何创建包含变量内容的字符 ...
- 笨办法学Python - 习题4: Variables and Names
1.习题 4: 变量(variable)和命名 学习目标:了解Python中变量的定义,学习定义简明易记的变量名 变量:变量是存储内存中的值,就是每定义一个变量就会在内存中开辟一个空间.基于变量的类型 ...
- 笨办法学Python - 习题3: Numbers and Math
目录 习题 3: 数字和数学计算 算术运算符 加分习题: 我的答案: 总结: 扩展: Python比较运算符 Python赋值运算符 Python位运算符 Python逻辑运算符 Python成员运算 ...
- 笨办法学python 习题14 优化过 遇到问题的请看
print "\t what's you name?"user_name = raw_input('>') from sys import argvscript, = arg ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
随机推荐
- Opencv——摄像头设置
VideoCapture capture(0);/*设置摄像头参数 不要随意修改capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度 capture.set( ...
- Spring源码分析(十二)FactoryBean的使用
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 一般情况下,Spring通过反射机制利用bean的class属性指定实现 ...
- HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- MapReduce -- 好友推荐
MapReduce实现好友推荐: 张三的好友有王五.小红.赵六; 同样王五.小红.赵六的共同好友是张三; 在王五和小红不认识的前提下,可以通过张三互相认识,给王五推荐的好友为小红, 给小红推荐的好友是 ...
- dataTable配置项说明
Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 官网地址:https://datatables.net/ 中文说明地址:http:/ ...
- python 第一课作用
1.使用while循环输入 1 2 3 4 5 6 8 9 10 x=0while x<10: x=x+1 if x==7: print(' ') continue print(x)#学 ...
- Go指南 - 笔记
Go指南 - 笔记 标签(空格分隔): Go Go指南 一.基础 1.包 每个Go程序都是由包构成的. 程序从main包开始运行. 包名与导入路径的最后一个元素一致 2.导入 分组导入:使用圆括号组合 ...
- c++ 绘制方框
知识点: GetStdHandle函数 FillConsoleOutputCharacter函数 SetConsoleCursorPosition函数 system函数 一. GetStdHandle ...
- OpenStack入门篇(二十二)之实现阿里云VPC的SDN网络
1.修改/etc/neutron/neutron.conf配置 [root@linux-node1 ~]# vim /etc/neutron/neutron.conf [defalut] ... co ...
- Oracle 表备份还原
方法1: create table mdmuser20120801 as select * from mdmuser 方法2: create table mdmuser20120801 as se ...