更多的变量和打印
我们将使用一个叫“格式化字符串(format string)”的东西.
字符串是非常好用的东西,所以再这个练习中你将学会如何创建包含变量内容的字符串。使用专门的格 式和语法把变量的内容放到字符串里,相当于来告诉python:“嘿,这是一个格式化字符串,把这些变量放到那几个位置。”一样的,即使你读不懂这些内容,只要一字不差地键入就可以了。
python格式化字符串的三种方法(%,format,f-string)_在java中可以使用string.format函数,python中可以使用%或format方法来格式-CSDN博客
最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中。
示例1
my_name = 'jayson AKA. 耶律'
my_age = 18
my_height = 70 #inch;1inch=2.54 centimeter
my_weight = 130
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Black'
print("Let's talk about %s." % my_name) #%s 格式化字符串
print("He's %d inches tall." % my_height) #%d 格式化整数
print("He's %d kg heavy." % my_weight)
print("Actually that's not too heavy")
print("He's got %s eyes and %s hair." % (my_eyes,my_hair))
print("His teeth are usually %s depending on the coffee." % my_teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight))
运行结果
加分题1 变量的运算
将英寸和磅通过python换成厘米和斤
my_name = 'jayson AKA. 耶律'
my_age = 18
my_height = 70 #inch;1inch=2.54 centimeter
my_weight = 140#pound;1磅=0.45kg
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Black'
print("Let's talk about %s." % my_name) #%s 格式化字符串
print("He's %d inches tall." % (my_height*2.54)) #%d 格式化整数
print("He's %d kg heavy." % (my_weight*0.9))
print("Actually that's not too heavy")
print("He's got %s eyes and %s hair." % (my_eyes,my_hair))
print("His teeth are usually %s depending on the coffee." % my_teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d." % (my_age, my_height*2.54 , my_weight*0.9, my_age +my_height*2.54 + my_weight*0.9))
在原来变量的基础上加上括号(变量*2.54)或(变量*0.9)即可让python自动计算变量
加分题2 修改变量名
Q:去掉代码中所有my_
A:修改变量名有快捷键
快捷键为:Ctrl+Shift+Alt+J
或者:Shift+F6
修改后
以下删除字符串或重复行的参考链接,后续有用到的话来参考
【Python教程】删除字符串中字符的四种方法_python删除字符串中的字符-CSDN博客
删除重复字符(Python)_本题要求编写程序,将给定字符串去掉重复的字符后,按照字符ascii码顺序从小到大排-CSDN博客
python学习之字符串去重的几种方法_python字符串去重-CSDN博客
PYTHON简单代码去除TXT文档重复行内容去重复_python删除txt中不整齐的数据的代码-CSDN博客
Python pandas处理(提取/删除)DataFrame中的重复行_data.duplicated(keep = false)-CSDN博客