python编程 从入门到实践 第二章 动手试一试参考答案 含运行结果

迟做总比不做好!

#2-1变量练习 动手试一试
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#将一条消息存储到变量中,再讲他打印出来
message = "Hello Python world!"
print(message)

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
#2-2将一条消息存储到变量中,并将其打印出来,再将变量的值修改为一条新消息,并将其打印出来。
message = "Hello Python world!"
print(message)

message = "Hello My Python world!"
print(message)

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
#2-3将用户的姓名存到一个变量中,并向该用户显示一条消息。
# 显示的消息应非常简单,如”Hello Eric,would you like to learn some python today?".
name = "eric"
print("Hello"+ " " + name.title() +","+"would you like to learn some python today?")

#title() 将首字母变为大写

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
#2-4将一个人名存储到一个变量中,再以小写,大写和首字母大小的方式显示这个人名
name = "eric wang"
print(name)
#将字符全部变为大写
print(name.upper())
#将字符全部变为小写
print(name.lower())
#将每个单词的首字母变为大写
print(name.title())

#2-5找一句你钦佩的名人说的名言,将这个人的姓名和他的名言打印出来。输出应类似于下面这样(包括括号):
# Albert Einstein once sail,"A person who never made a mistake tried anything new."
#读书之法,在循序而渐进,熟读而精思。——朱熹
#Reading of the law, in an orderly and progressive, familiar with the fine si. -- Zhu Xi
name = "zhu xi"
saying ="reading of the law,in an orderly and progressive,familiar with the fine si"
print(name.title()+ " " +"once said," +'"Read of the law, in an orderly and progressive,familiar with the fine si."')

#2-6 名言2:重复练习2-5,但将名人的姓名存储在变量famous_person 中,再创建要显示的消息,并将其存储在变量message中,然后打印这条消息
#读书之法,在循序而渐进,熟读而精思。——朱熹
#Reading of the law, in an orderly and progressive, familiar with the fine si. -- Zhu Xi
famous_person= "zhu xi"
#单双引号的使用,但需要双引号时,用单引号扣住
message = '"Read of the law, in an orderly and progressive,familiar with the fine si."'
print(famous_person.title()+" "+"once said"+" "+message)

#2-7剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合“\t"和"\n"各一次。
# 打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip()、rstrip()和strip()对人名进行处理,并将结果打印出来。
#\t:制表符 \t 的作用是跳格,即跳到下一个“制表位置”(也有说是TAB位置),在我们所用的系统中一个“制表区”占8列。“下一制表位置”从第9列开始
#\n:换行
name = "\tzhu xi\t\n"
print(name)
print(123456789)
#rstrip():删除字符串末尾空格;lstrip:删除字符串开头空格;strip:删除字符串两端空白
#剔除末尾空格
print(name)
print(name.rstrip())
#剔除两端空格
print(name)
print(name.strip())
#剔除开头空格
print(name)
print(name.lstrip())

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值