Python学习笔记

工作需要熟悉一下Python, 在看Learn Python the hard way, 记点笔记

--------------------------------------------------------------------------------------------------------

 What is the difference between %r and %s? 

    We use %r for debugging, since it displays the “raw” data of the variable, but we use %s andothers for displaying to users.

Example:

>>> x = "There are %d types of people." % 10
>>> print "said %r" % x
said 'There are 10 types of people.'
>>> print "said %s" % x
said There are 10 types of people.

What’s the point of %s and %d when you can just use %r?

    The %r is best for debugging, and the other formats are for actually displaying variables to users.


Why do you put ' (single- quotes) around some strings and not others?

    Mostly it’s because of style, but I’ll use a single- quote inside a string that has double- quotes.

>>> print "I also said: '%s'." % y
I also said: 'There are 10 types of people.'.


* 在print中的用法:

>>> print "." * 10 # what'd that do?
..........


,在print中的用法: 表示当前行还没结束, 下面一个print的string还会放在同一行中, 比如

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"

 # watch that comma at the end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12

会返回

$ python ex2.py
..........
Cheese Burger

而如果把 end6 后面的逗号去掉, 则会变成

$ python ex2.py
..........
Cheese
Burger

print """ 可以用来打印任意长度的字符串,比如

>>> print """
... Hi hello world I
... I like something good, bad, etc.
... heheh
... hahaha ..... example!
... """

Hi hello world I
I like something good, bad, etc.
heheh
hahaha ..... example!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值