当前位置:网站首页>简单计算器,单层循环输出乘法表,字符串方法的使用,格式化输出
简单计算器,单层循环输出乘法表,字符串方法的使用,格式化输出
2022-07-31 05:19:00 【m0_59138290】
写一个简单(±*/)的计算器, 确保输入的都是数字(如果不是数字,让它重新输入)
输入格式必须是: 7+8 => 输出格式是 7 + 8 = 15
data = input("请输入要进行运算的式子:") x = int(data[0]) y = data[1] z = int(data[2]) if y == "+": print("{}+{}={}".format(x, z, x+z)) elif y == "-": print("{}-{}={}".format(x, z, x-z)) elif y == "*": print("{}x{}={}".format(x, z, x * z)) elif y == "/": print("{}/{}={}".format(x, z, x / z))
while单层循环完成9 * 9乘法表
i = 1 j = 1 while i < 10: print(i, "x", j, "=", i * j, end="\t") if i == j: j = 0 i += 1 print() j += 1
str字符串中的strip, replace, split, partition, expandtabs, join
center, ljust, rjust方法
strip 返回字符串的剪裁版本。
txt = " banana "
x = txt.strip()
print("of all fruits", x, "is my favorite")
replace 返回字符串,其中指定的值被替换为指定的值。
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
split 在指定的分隔符处拆分字符串,并返回列表
txt = "welcome to China"
x = txt.split()
print(x)
partition 返回元组,其中的字符串被分为三部分。
txt = "I could eat bananas all day"
x = txt.partition("bananas")
print(x)
expandtabs 设置字符串的 tab 尺寸。
txt = "H\te\tl\tl\to"
x = txt.expandtabs(2)
print(x)
join 把可迭代对象的元素连接到字符串的末尾。
myTuple = ("Bill", "Steve", "Elon")
x = "#".join(myTuple)
print(x)
center 返回居中的字符串。
txt = "banana"
x = txt.center(20)
print(x)
ljust 返回字符串的左对齐版本。
txt = "banana"
x = txt.ljust(20)
print(x, "is my favorite fruit.")
rjust 返回字符串的右对齐版本。
txt = "banana"
x = txt.rjust(20)
print(x, "is my favorite fruit.")
格式化输出:
姓名 年龄 性别 家庭住址
xxx xxxxlist_data = [{name: xxx, age: xxx, gender: xxx, address}, .....] 包含居中对齐,向左对齐, 向右对齐
list_data = [{"name": "张三", "age": "20", "gender": "男", "address": "西安市长安区"}]
print(list_data[0].keys())
for i in list_data[0].keys():
print(i.center(20), end=" ")
print()
for i in list_data[0].values():
print(i.center(19), end=" ")
边栏推荐
猜你喜欢
Chemical Reagent Phospholipid-Polyethylene Glycol-Amino, DSPE-PEG-amine, CAS: 474922-26-4
vs2022 xlua 集成第三方库编译报错Generator Visual Studio 15 2017 could not find any instance of Visual Studio.
滑动窗口法
DingTalk Enterprise Internal-H5 Micro Application Development
Learning and understanding of ROS service programming
钉钉企业内部-H5微应用开发
浅谈音视频开发入门基础及进阶资源分享
ES6-模块
Incredibuild 宣布支持 Yocto
Picture-in-Picture API in the browser
随机推荐
ES6-模块
mPEG-DMPE Methoxy-polyethylene glycol-bismyristyl phosphatidylethanolamine for stealth liposome formation
Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400
【源码笔记】痛苦来源于比较——什么是相等,hashCode() 和 equals(Object)
mobaxterm 编码问题解决
滴滴被罚超80亿!收集并泄露1.07亿条乘客人脸识别信息
mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids
Learning and understanding of ROS service programming
ES6-数组
通过js禁止ctrl+滚轮放缩浏览器页面,禁止用手势放大
【Rhapsody学习笔记】3:Dishwasher
螺旋矩阵Ⅱ
2021年开发人员的绊脚石:构建时间长
Attention based ASR(LAS)
国际站卖家大促攻略,只需要做好这几件事
面试总爱问的一个问题,你为什么离职上一份工作?
多线程截取视频为每帧
IDEA控制台不能输入信息的解决方法
力扣.三数之和/四数之和
DSPE-PEG-Thiol DSPE-PEG-SH phospholipid-polyethylene glycol-thiol liposome for later use