# 2-1 简单消息
nice = "Hello World!"
print(nice)
# 2-2 多条简单消息
nice = "Hello World!"
print(nice)
nice = "Hello Python!"
print(nice)
# 2-3 个性化消息:
name = "eric"
message = "Hello " + name.title() + ", would you like to learn some Python today?"
print(message)
# 2-4 调整名字的大小写
name = "aLIce"
print(name.title())
print(name.upper())
print(name.lower())
# 2-5+2-6 名言
first_name = "albert"
last_name = "einstein"
full_name = first_name.title() + " " + last_name.title()
message = full_name + ' once said, "A person who never made a mistake never tried anything new."'
print(message)
# 2-7 剔除人名中的空白
print(" bob ")
print("\tbob")
print("\n bob")
print("name:\nbob\nalice\nliming")
print("name:\n\tbob\n\talice\n\tliming")
name = " alice "
print(name)
print(name.lstrip())
print(name.rstrip())
print(name.strip())
# 2-8 数字8
print(2 + 6)
print(10 - 2)
print(2 * 4)
print(16 / 2)
# 2-9 最喜欢的数字
num = 8
message = "My favorite number is " + str(num)
print(message)
# 2-10 添加注释
# 2-11 Python之禅
《Python编程:从入门到实践》习题答案——第2章 变量和简单数据类型
于 2023-09-07 08:26:12 首次发布