《笨方法学Python》第四期:字符串练习

Python系列之笨方法学Python是我学习《笨方法学Python》—Zed A. Show著
的学习思路和理解,如有不如之处,望指出!!!

文章主要分为三个部分:

  1. 原文—摘录至《笨方法学Python》第三版
  2. 学习中遇到的问题
  3. 问题的解决方法
  4. 附加练习

原文—摘录至《笨方法学Python》第三版

ex6.py & ex8.py
# ex6.py

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?! %r " 

print joke_evaluation % hilarious
w="This is the left side of ..."
e="a sting with a right side."
print w+e 

# ex8.py

fomatter="%r %r %r %r"

print fomatter %(1,2,3,4)
print fomatter %("one","two","three","four")
print fomatter %(True,False,False,True)
print fomatter %(fomatter,fomatter,fomatter,fomatter)
print fomatter %(
	"I had this thing.",
	"That you could type up right."
	"But it didn't sing."
	"So I said goodnight."
	)

遇到的问题

  1. ex8.py 的代码中 %r打印出来的是单引号,但是实际上用的双引号
    python会用最有效的方式打印出字符串,而不是完全按照你写的方式来打印。
  2. TrueFalse是Pyhthon的关键字,用来表示真和假的概念。如果加了引号,就变成了字符串,无法实现原来的功能。
  3. %r的作用
    %r是用来调试(debug)比较好,因为它会显示变量的原始数据,而%s%d等符号是用来向用户显示输出的。

附加练习

以下摘录自原文

  1. 每一行加一段注释
  2. 最后一行输出既有单引号又有双引号,你觉得这背后的机制是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值