Python学习笔记——Strings and Text

本文通过一系列示例介绍了Python的基础用法,包括变量定义、字符串格式化、条件判断等,并解释了不同字符串格式化符号的用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows PowerShell
版权所有 (C) 2009 Microsoft Corporation。保留所有权利。

PS D:\> python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> round(1.7333)
2.0
>>> my_age=35 #not a lie
>>> my_height=74 #inches
>>> my_weight=180 #lbs
>>> print "If I add %d,%d, and %d I get %d." %(
... my_age,my_height,my_weight,my_age+my_height+my_weight)
If I add 35,74, and 180 I get 289.
>>> my_teeth='white'
>>> print "His teeth are usually %s depending on the coffee." %my_teeth
His teeth are usually white depending on the coffee.
>>> x="There are %d types of people." %10
>>> print x
There are 10 types of people.
>>> binary="binary"
>>> do_not="don't"
>>>
>>> y="Those who know %s and those who %s." %(binary,do_not)
>>>
>>> print y
Those who know binary and those who don't.
>>> print "I said: %r." %x
I said: 'There are 10 types of people.'.
>>> print "I also said:'%s'." %y
I also said:'Those who know binary and those who don't.'.
>>>
>>>
>>>
>>> hilarious=False
>>>
>>> joke_evaluation="Isn't that joke so funnny?! %r"
>>>
>>> print joke_evaluation % hilarious
Isn't that joke so funnny?! False
>>>
>>>
>>> w="This is the left side of ..."
>>> e="a string with a right side."
>>>
>>> print w+e
This is the left side of ...a string with a right side.
>>>
>>> print "I said: %r." %x
I said: 'There are 10 types of people.'.
>>>
>>>
>>> x = "There are %d types of people." % 10
>>>
>>> print "I also said: '%s'." % x
I also said: 'There are 10 types of people.'.
>>> print "I said: %r." %x
I said: 'There are 10 types of people.'.
>>> print "I said: %s." %x
I said: There are 10 types of people..

>>>



>>> x = "There are %d types of people." % 10
>>>
>>> print "I also said: '%s'." % x
I also said: 'There are 10 types of people.'.
>>> print "I said: %r." %x
I said: 'There are 10 types of people.'.
>>> print "I said: %s." %x
I said: There are 10 types of people..
>>> x = "There are %s types of people." % 10
>>> print x
There are 10 types of people.
>>> print "I said: %s." %x
I said: There are 10 types of people..
>>> x = "There are %d types of people." % x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str

>>>
>>>
>>> print "I said: %s." %a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

>>> print "I said: %s." %3
I said: 3.
>>> x = "There are %d types of people." % b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> x = "There are %d types of people." % 3
>>> print x
There are 3 types of people.
>>>



What is the difference between %r and %s?
Use the %r for debugging, since it displays the "raw" data of the variable, but the others are used for displaying to users.
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值