笨办法学Python - 习题8-10: Printing & Printing, Printing

目录

  • 1、习题 8: 打印,打印
  • 2、习题 9: 打印,打印,打印
  • 3、习题 10: 那是什么?
    • 3.1、转义序列:
  • 4、习题总结:

1、习题 8: 打印,打印

学习目标:继续学习 %r 的格式化输出。

习题八中的练习代码是:

#! -*-coding=utf-8 -*-

formatter = "%r %r %r %r %r "

print formatter % (1, "hello", [1,2,3], (1,2,3), {"name":"jack"})

print formatter % ("one", "two", "three", "four", "five")

print formatter % (True, False, True, False, False)

print formatter % (
    "I had this thing. ",
    "That you could type up right. ",
    "But it didn't sing. ",
    "So I said doognight. ",
    "Hello world."
)

上述代码的运行结果是:

C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo8/Exer8-1.py
1 'hello' [1, 2, 3] (1, 2, 3) {'name': 'jack'}
'one' 'two' 'three' 'four' 'five'
True False True False False
'I had this thing. ' 'That you could type up right. ' "But it didn't sing. " 'So I said doognight. ' 'Hello world.' 

Process finished with exit code 0

注意:上述代码说明两个点,一个是%r 的作用,是占位符,可以将后面给的值按原数据类型输出(不会变),支持数字、字符串、列表、元组、字典等所有数据类型。

还有一个需要注意的就是代码的最后一行:

print formatter % (
    "I had this thing. ",
    "That you could type up right. ",
    "But it didn't sing. ",
    "So I said doognight. ",
    "Hello world."
)
'I had this thing. ' 'That you could type up right. ' "But it didn't sing. " 'So I said doognight. ' 'Hello world.' 

最后输出的语句中既有单引号,也有双引号。原因在于 %r 格式化字符后是显示字符的原始数据。而字符串的原始数据包含引号,所以我们看到其他字符串被格式化后显示单引号。 而这条双引号的字符串是因为原始字符串中有了单引号,为避免字符意外截断,python 自动为这段字符串添加了双引号。

2、习题 9: 打印,打印,打印

学习目标:了解 \n 的含义

习题九中的练习代码是:

#! -*-coding=utf-8 -*-

days = "Mon Tue Wed Thu Fri Sat Sun"

months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"

print "Here are the days: ",days
print "Here are the months: ",months

print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo9/Exer9-1.py
Here are the days:  Mon Tue Wed Thu Fri Sat Sun
Here are the months:  Jan
Feb
Mar
Apr
May
Jun
Jul
Aug

There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.

Process finished with exit code 0

上述代码有两个点需要注意下,一个是换行符 \n ,一个是注释符三引号。换行符就是避免代码过长影响阅读性而手动进行代码换行操作,\n 其实只是一个字符,类似的还有制表符 \t ,具体的更过的换行符知识请见下一题。

3、习题 10: 那是什么?

学习目标:了解 \n 的含义,了解 ??的含义

首先来了解一下两种让字符串扩展到多行的方法:

  1. 换行符 \n (back-slash n ):两个字符的作用是在该位置上放入一个“新行(new line)”字符
  2. 双反斜杠(double back-slash) ??:这两个字符组合会打印出一个反斜杠来

3.1、转义序列:

下面介绍下再Python中常见的转义序列:

转义字符 描述
?(在行尾时) 续行符
? \ 反斜杠符号
单引号
" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数yy代表的字符,例如:\o12代表换行
\xyy 十进制数yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通格式输出

在字符串中,有时需要包含一些特殊的符号,但是有些符号不能直接输出,就需要使用转义序列

举个栗子:

在打印输出一句话时,可能同时包含单引号和双引号,这种情况下在print 语句中不加其他操作肯定是会出错的。/手动滑稽

在这种情况下,我们有两种方法解决此问题;

  1. 使用转义序列
  2. 使用注释符-三引号

使用转义序列:

使用注释符:

总结:

转义序列就是将在print 下无法正常显示的字符打印出来,比如说打印 ?, 换行等。

再来认识一下转义字符 \b 的作用:作用是退格,就是删除前一个字符的意思

[1547697550481](https://img2018.cnblogs.com/blog/1324118/201901/1324118-20190117135049172-166563149.png " \b作用 - 退格、删除")

转义字符 \r :也是换行作用,与 \n 不同的是光标的位置:\n 在下一行开头,\r 在本行的开头

print u"你好吗?\n朋友"
print u"——分隔线——"
print u"你好吗?\r朋友"
print "hello \rworld"
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo10/Exer10-1.py
你好吗?
朋友
——分隔线——
朋友
world
Process finished with exit code 0

从上面代码可以看出来,\r 是回车,是只会打印\r 后面的内容,前面的内容自动忽略。

具体的其他制表符运用还得自己练习。

习题十中的练习代码是:

#! -*-coding=utf-8 -*-

tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat

上述代码的运行结果为:

C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo10/Exer10-1.py
    I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.

I'll do a list:
    * Cat food
    * Fishies
    * Catnip
    * Grass

Process finished with exit code 0

从上面可以看出转义字符的含义。?t 是水平制表符, ??是用于打印 ?的。

如果将转义字符和格式化输出相结合,则会生成一个更复杂的格式,举个栗子:

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""

print "heloo %r " % fat_cat

print "----------------------------"

print "heloo %s " % fat_cat
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo10/Exer10-1.py
heloo "\nI'll do a list:\n\t* Cat food\n\t* Fishies\n\t* Catnip\n\t* Grass\n"
----------------------------
heloo
I'll do a list:
    * Cat food
    * Fishies
    * Catnip
    * Grass

Process finished with exit code 0

从上面的代码中可以更好的体现出格式化输出的占位符 %r 和 %s 之间的区别。%r 是输出原格式,%s是输出字符串。

4、习题总结:

上面的三道习题,前两题只是之前的知识回顾,就是格式化输出的应用实践,后面习题10是说明了常见转义字符的作用,和一些续航建的转义字符的含义。结合格式化输出和转义字符可以生成更复杂的格式。重点理解%s 和 %r 的作用。

原文地址:https://www.cnblogs.com/csyxf/p/10281835.html

时间: 2024-07-28 15:11:51

笨办法学Python - 习题8-10: Printing & Printing, Printing的相关文章

笨办法学Python - 习题3: Numbers and Math

Exercise2是注释和井号 Comments and Pound Characters 具体详情请参考习题一,这里就不在做过多的赘述. 习题 3: 数字和数学计算 学习目标:了解Python中常用的算术运算符,并了解运算符之间的先后运算顺序 在各大常用的计算机语言中都有常见的算术运算符,Python也是大同小异,下面我们来了解一下Python中常见的算术运算符: 算术运算符 以下假设变量x = 10 ,y = 20 运算符 描述 实例 + 加 - 两个对象相加 x+y = 30 - 减 -

笨办法学python 习题42 加分练习

3.创建一个新版本,里边使用两个 class,其中一个是 Map ,另一个是 Engine .提示: 把 play 放到 Engine 里面.. #coding=utf-8from sys import exitfrom random import randint class Map(object):    def __init__(self):               self.quips = ["You died. You kinda suck at this.","

笨办法学python 习题14 优化过 遇到问题的请看

print "\t what's you name?"user_name = raw_input('>') from sys import argvscript, = argv prompt = '>' print "\t hi %s,I'm the %s script"%(user_name,script)print "\t I'd like to ask you a few questions" print "\t Do

笨办法学Python 练习13和14

原题: 1 from sys import argv 2 3 script, first, second, third = argv 4 5 print "The script is called:", script 6 print "Your first variable is:", first 7 print "Your second variable is:", second 8 print "Your third variabl

笨办法学 Python (Learn Python The Hard Way)

最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 3: 数字和数学计算 习题 4: 变量(variable)和命名 习题 5: 更多的变量和打印 习题 6: 字符串(string)和文本 习题 7: 更多打印 习题 8: 打印,打印 习题 9: 打印,打印,打印 习题 10: 那是什么? 习题 11: 提问 习题 12: 提示别人

习题 5: 更多的变量和打印 | 笨办法学 Python

一. 简述 “格式化字符串(format string)” -  每一次你使用 ' ’ 或 " " 把一些文本引用起来,你就建立了一个字符串. 字符串是程序将信息展示给人的方式. 二. 代码 1 #!usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 # Author: xixihuang 5 # Date : 2016/08/31 09:52 AM 6 # Desc : 习题5:更多的变量与打印 7 # 键入更多的变量并且将它们打印出来.这

《笨办法学python第三版》习题26,原错误代码及正确代码

#import ex25 1 def break_words(stuff): 2 """This function will break up words for us.""" 3 words = stuff.split(' ') 4 return words 5 6 def sort_words(words): 7 """Sorts the words.""" 8 return sor

笨办法学Python(一)

习题 1: 第一个程序 你应该在练习 0 中花了不少的时间,学会了如何安装文本编辑器.运行文本编辑器.以及如何运行命令行终端,而且你已经花时间熟悉了这些工具.请不要跳过前一个练习的内容直接进行下面的内容,这也是本书唯一的一次这样的警示. 1 print "Hello World!" 2 3 print "Hello Again" 4 5 print "I like typing this." 6 7 print "This is fun

笨办法学Python(二十八)

习题 28: 布尔表达式练习 上一节你学到的逻辑组合的正式名称是"布尔逻辑表达式(boolean logic expression)".在编程中,布尔逻辑可以说是无处不在.它们是计算机运算的基础和重要组成部分,掌握它们就跟学音乐掌握音阶一样重要. 在这节练习中,你将在 python 里使用到上节学到的逻辑表达式.先为下面的每一个逻辑问题写出你认为的答案,每一题的答案要么为 True 要么为 False.写完以后,你需要将python 运行起来,把这些逻辑语句输入进去,确认你写的答案是否