Python2.7学习笔记 Day2

Python2.7学习笔记 Day2

字符串String和文本

代码示例与注释:

#-*- coding: UTF-8 -*-
x = "There are %d types of people." % 10
#这里%d指代整数10
binary = "binary"
#定义一个变量,是字符串
do_not = "don't"
#定义一个变量,是字符串
y = "Those who know %s and those who %s." % (binary, do_not)
#y是字符串,知道二进制和不知道二进制的人

print x
print y

print "I said: %r." % x 
#打印 I said x。 此处%r指代字符串x的内容
print "I also said: '%s'." % y 
#打印 I also said:'y'. 此处%s指代字符串y 

hilarious = False
#定义一个变量hilarious为“假”
joke_evaluation = "Isn't that jole so funny?! %r"
#定义一个变量joke_evaluation为 。。。 **此处的%r是什么意思???**

print joke_evaluation % hilarious

w = "This is the left side of..."
e = "a string with a right side."

print w + e 
#字符串可以相加,相加就是接到一起

输出的结果:

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 jole so funny?! False
This is the left side of...a string with a right side.

在做这个练习的时候,出现了一个报错,提示为IndentationError: unexpected indent。对于此错误,最常见的原因是:代码没有对齐。python编写代码时,不空格的代码是一组代码块,空一格的是一组代码块,空两格的是一组代码块,代码块区分是按照空几个格来区分的。

解决办法:在Notepad++里,视图 -> 显示符号 -> 显示空格与制表符,显示出所有空格与制表符就可以找到哪里没有对齐了

字符串String和文本的巩固练习1

练习例子:

# coding = utf-8
print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
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 + end10 + end11 + end12
#此处的 , 起到 + 的作用 把两个单词连接起来了

输出结果:

Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Buger

字符串String和文本的巩固练习2

练习代码:

# -- coding: UTF-8 --
formatter = "%r %r %r %r"
#首先定义了一个变量叫formatter,它是个字符串,由4个任意的东西构成

print formatter % (1, 2, 3, 4)
#打印这个变量,给这个变量4个内容,分别为1234
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, True, False)
print formatter % (formatter, formatter, formatter, formatter)
#此处意义重大,当你给fomatter这个变量赋予4个内容时,它就有内容
#如果没有赋予其内容,formatter代表一个内容为 %r %r %r %r 的字符串
print formatter % (
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
    )

运行结果:

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

为什么最后一行既有双引号也有单引号呢?

字符串String和文本的巩固练习3

练习代码:

# -- coding: UTF-8 --
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\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.
"""

输出结果:

Here are the days:  Mon Tue Wed Thu Fri Sat Sun
Here are the months:  Jan
Feb
Mar
Apr
May
Jun
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.

从这里可以知道,\n指的是下一行的意思。可以用两个"""来括住一段话来打印出来。

字符串String和文本的巩固练习4

转义符号\
那些会引起“歧义”或阻挠python判断语句的符号可以加上\来转义。
例如: \n表示下一行
\t表示一个制表符
print “I want to print \\”中,\\代表后一个\是字符串,这句命令的结果是I want to print \
下面由一个例子来说明:

# -- 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

运行结果是:

        I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.

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

新的篇章,把数据读到你的程序当中去

一般软件做的事情主要就是下面几条:

1.接受人的输入。
2.改变输入。
3.打印出改变了的输入。
学习一个新的语法raw_input
练习代码如下:

# -- coding: UTF-8 --
print "How old are you?"
age = raw_input()   #输入
print "How tall are you?"
height = raw_input()
print "How much do you weigh?"
weight = raw_input()

print "So, you're %r old, %r tall and %r heaby." % (age, height, weight)

运行结果如下:

How old are you?
28
How tall are you?
181
How much do you weigh?
85
So, you're '28' old, '181' tall and '85' heaby.

raw _input()的用法

对于 raw_input 而言,还可以让它显示出一个提示,从而告诉别人应该输入什么东西。可以在 () 之间放入一个想要作为提示的字符串,如下所示:
y = raw_input("Name? ")
这句话会用 “Name?” 提示用户,然后将用户输入的结果赋值给变量 y。这就是我们提问用户并且得到答案的方式。
上一个练习可以使用 raw_input 重写一次。所有的提示都可以通过
raw_input 实现。
练习代码如下:

上一个练习可以使用 raw_input 重写一次。所有的提示都可以通过
raw_input 实现。

运行结果为:

How old are you?28
How tall are you?180
How much do you weight?82
So, you are '28' old, '180' tall and '82' heavy.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值