练习5--更多变量和打印

1 字符串的初步了解

  • 定义:你让计算机呈现给人们的内容
  • 创建:用引号引用一个文本的过程就创建了一个字符串,单引号双印号都行
  • 操作:打印字符串、将字符串保存到文件、将字符串发送到网络服务器等

2 创建包含变量的字符串: 变量名 = ' 字符串 '       (可以是单引号可以是双引号,一般都用单引号)

3 字符串的格式化输出方式:print(f"This is {变量名}")

  • 注:{} 是为了把我们定义的变量嵌入到字符串你”This is “中
  •        f 表示这个字符串需要被格式化,并且把我们定义的变量放在{}所在的位置

4 变量名的命名规则一:必须以字母开头

5 round()函数的作用:给浮点数四舍五入取整数

6 单位换算:1英尺=30.48厘米   1镑=0.45359237千克

7 代码

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print(f"Let's talk about {my_name}.")
print(f"He's {my_height} inches tall.")
print(f"he's {my_weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"He's teeth are usually {my_teeth} depending on the coffee.")
# this line is tricky,try not to get it exactly right
total = my_age + my_height + my_weight
print(f"If I add {my_age},{my_height},add {my_weight} T get {total}.")

8 运行结果

PS E:3_work4_python2_code2_LearnPythonTheHardWay> python ex5.py
Let's talk about Zed A. Shaw.
He's 74 inches tall.
he's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
He's teeth are usually White depending on the coffee.
If I add 35,74,add 180 T get 289.

9 python2输出字符串的方式: print "Let's talk about %s." % my_name

  • 注意:%s(字符串)、%d(整数或者浮点数)、%r(不管什么都打印出来)都是格式化控制工具,它们告诉 Python 把右边的变量带到字符串中,并且把变量值放到 %s 所在的位置上。
  •            python3里面通过这种方式也可以打印出来
原文地址:https://www.cnblogs.com/luoxun/p/13132031.html