高级编程技术 课后作业二

本文通过一系列简单的Python代码示例介绍了基本的字符串操作方法,包括变量赋值、字符串拼接、大小写转换、去除空白字符等,同时展示了基本的数学运算。

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

2-1 简单消息:将一条消息存储到变量中,再将其打印出来
>>> message = "Hello World! "
>>> print(message)
Hello World!

2-2 多条简单消息:将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来
>>> message = "Hello World! "
>>> print(message)
Hello World!
>>> message = "Hello Python! "
>>> print(message)
Hello Python!

2-3 个性化信息:将用户的姓名存到一个变量中,并向该用户显示一条消息
>>> name = "Eric"
>>> print("Hello "+name+", would you like to learn some Python today?")
Hello Eric, would you like to learn some Python today?


2-4 调整名字的大小写:将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示人名
>>> name = "alice smith"
>>> print(name.title())
Alice Smith
>>> print(name.upper())
ALICE SMITH
>>> print(name.lower())
alice smith


2-5 名言:找一句你钦佩的名人说的名言
>>> print('Albert Einstein once said,"A person who never made a mistake never tried anything new."')
Albert Einstein once said,"A person who never made a mistake never tried anything new."


2-6 名言2:将名人的姓名存储在变量famous_person中,再创建要显示的消息,并将其存储在变量message中,然后打印这条消息
>>> famous_person = "Albert Einstein"
>>> message = "A person who never made a mistake never tried anything new."
>>> print(famous_person + ' once said,"' + message + '"')
Albert Einstein once said,"A person who never made a mistake never tried anything new."


2-7 剔除人名中的空白:存储一个个人名,并在其开头和末尾都包含一些空白字符,打印这个人名然后使用剔除函数对人名进行处理,并将结果打印出来
>>> name = "\t\t Albert Einstein \n"
>>> print(name)
                 Albert Einstein

>>> print(name.lstrip())
Albert Einstein

>>> print(name.rstrip())
                 Albert Einstein
>>> print(name.strip())
Albert Einstein

2-8 数字8:编写4个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是8,使用print语句
>>> print(3 + 5)
8
>>> print(10 - 2)
8
>>> print(2 * 4)
8
>>> print(int(16 / 2))
8

2-9 最喜欢的数字:将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来
>>> number = 8086
>>> print("My favorite number is " + str(number) + ".")
My favorite number is 8086.


2-10 添加注释:选择你编写的两个程序,在每个程序中都至少添加一条注释
# 向大家问好
message = "Hello Python people! "
print(message)
'''
4个分别使用加法、减法、乘法和除法运算的表达式
结果都是8,且都使用print语句
'''
print(3 + 5)
print(10 - 2)
print(2 * 4)
print(int(16 / 2))


2-11 Python之禅:在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!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值