一 2.3章节习题
1 将用户的姓名存到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。
2 调整名字的大小写: 将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。
3 名言: 找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):
Theresa Mary May once said, “A person who never made a mistake never tried anything new.”
4 名言2: 重复练习2-5,但将名人的姓名存储在变量famous_person 中,再创建要显示的消息,并将其存储在变量message 中,然后打印这条消息。
5 剔除人名中的空白: 存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t" 和"\n" 各一次。 打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。
——《Python编程:从入门到实践》
二 程序代码
1 个性化消息
直接在这里联系敲写代码,
name_personal = "Theresa Mary May"
message = "What can I learn from my faults?"
print(“Hello”,name_personal,message)
复制,粘贴,运行
File "/Users/XX/Desktop/python_work/name_personal_message.py", line 3
print(“Hello”,name_personal,message)
^
SyntaxError: invalid character in identifier
[Finished in 0.0s with exit code 1]
[cmd: ['/usr/local/bin/python3', '-u', '/Users/XX/Desktop/python_work/name_personal_message.py']]
[dir: /Users/XX/Desktop/python_work]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
中文逗号➡️英文逗号后
File "/Users/XX/Desktop/python_work/name_personal_message.py", line 3
print("Hello“,name_personal,message)
^
SyntaxError: EOL while scanning string literal
中文双引号➡️英文双引号
File "/Users/XX/Desktop/python_work/name_personal_message.py", line 3
print("Hello”,name_personal,message)
^
SyntaxError: EOL while scanning string literal
[Finished in 0.0s with exit code 1]
好吧,其实我只改了左边的双引号,改了右边之后
Hello Theresa Mary May What can I learn from my faults?
[Finished in 0.0s]
加入标点符号
name_personal = "Theresa Mary May"
message = "What can I learn from my faults?"
print("Hello ,",name_personal,",",message)
运行结果
Hello , Theresa Mary May , What can I learn from my faults?
[Finished in 0.0s]
2 调整名字的大小写
name_personal = "theResa mary MAY"
print("大写",name_personal.upper())
print("小写",name_personal.lower())
print("首字母大写",name_personal.title())
大写 THERESA MARY MAY
小写 theresa mary may
首字母大写 Theresa Mary May
[Finished in 0.0s]
3 名言
name_personal = "Theresa Mary May"
message_1 = "I feel as certain today as I did three years ago that in a democracy, if"
message_2 = "you give people a choice you have a duty to implement what they decide. I have done my best to do that."
print(name_personal,"once said,''",message_1,message_2,".''")
Theresa Mary May once said,'' I feel as certain today as I did three years ago that in a democracy, if you give people a choice you have a duty to implement what they decide. I have done my best to do that. .''
[Finished in 0.0s]
4 名言2
famous_person = "Theresa Mary May"
message_1 = "I feel as certain today as I did three years ago that in a democracy, if"
message_2 = "you give people a choice you have a duty to implement what they decide. I have done my best to do that."
print(famous_person,"once said,''",message_1,message_2,".''")
Theresa Mary May once said,'' I feel as certain today as I did three years ago that in a democracy, if you give people a choice you have a duty to implement what they decide. I have done my best to do that. .''
[Finished in 0.0s]
5 过滤空白
famous_person = "\t Theresa \n Mary \t May \t"
print("过滤前“,”(左侧分割线|",famous_person,"|右侧分割线)")
print("过滤左侧空白后“,”(左侧分割线|",famous_person.lstrip(),"|右侧分割线)")
print("过滤右侧空白后“,”(左侧分割线|",famous_person.rstrip(),"|右侧分割线)")
print("过滤两侧空白后“,”(左侧分割线|",famous_person.strip(),"|右侧分割线)")
过滤前“,”(左侧分割线| Theresa
Mary May |右侧分割线)
过滤左侧空白后“,”(左侧分割线| Theresa
Mary May |右侧分割线)
过滤右侧空白后“,”(左侧分割线| Theresa
Mary May |右侧分割线)
过滤两侧空白后“,”(左侧分割线| Theresa
Mary May |右侧分割线)
[Finished in 0.0s]
【发现】strip()并不能过滤掉字符串中间的换行符。