2-2. 将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来:
>>> msg = "Hello World"
>>> print(msg)
Hello World
2-4. 将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名:
>>> name = "sam" # 把人名存到一个变量中
>>> print(name.upper()) # 输出大写
SAM
>>> print(name.lower()) # 输出小写
sam
>>> print(name.capitalize()) # 输出首字母大写
Sam
2-6. 重复练习2-5,但将名人的姓名存储在变量famous_person 中,再创建要显示的消息,并将其存储在变量message 中,然后打印这条消息
>>> famous_person = "africamonkey"
>>> message = "%s is a dalao" % famous_person
>>> print(message)
africamonkey is a dalao
2-7. 存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合”\t” 和”\n” 各一次。打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。
>>> name = "\tafrica monkey\n"
>>> print(name)
africa monkey
>>> print(name.lstrip())
africa monkey
>>> print(name.rstrip())
africa monkey
>>> print(name.strip())
africa monkey
2-11. 在Python终端会话中执行命令import this ,并粗略地浏览一下其他的指导原则。
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!