当前位置:网站首页>简单计算器,单层循环输出乘法表,字符串方法的使用,格式化输出
简单计算器,单层循环输出乘法表,字符串方法的使用,格式化输出
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 += 1str字符串中的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=" ")
边栏推荐
- 浏览器中的画中画(Picture-in-Picture)API
- cenos版本查询,及离线安装zip和unzip
- Log jar package conflict, and its solution
- 911崩了,自养号测评环境IP有哪些更好的选择
- The content of the wangeditor editor is transferred to the background server for storage
- 螺旋矩阵Ⅱ
- Tensorflow steps on the pit while using it
- 十分钟教你玩转分支语句!!!!!小白速进,新手福利!!
- 虚拟机查看端口号进程
- 【源码笔记】痛苦来源于比较——什么是相等,hashCode() 和 equals(Object)
猜你喜欢

Attention based ASR(LAS)

windows下mysql忘记密码登录,并创建用户

ES6-class类

DingTalk Enterprise Internal-H5 Micro Application Development

能否更上一层楼?探究 CMake 争论

超详细!!!让你通透数组!!!初学复习不迷路!!

The solution to the IDEA console not being able to enter information

Log jar package conflict, and its solution

ImportError: cannot import name 'Xxxx' from partially initialized module 'xx.xx.xx'

Unity转微信小游戏与JS交互
随机推荐
MySQL master-slave switching steps
超详细!!!让你通透数组!!!初学复习不迷路!!
ES6-02-let和const关键字
ES6-新增的基本数据:Symbol
能否更上一层楼?探究 CMake 争论
变更管理与 DevOps —— 二者同时进行吗?
滑动窗口法
UE5 最新动态虚幻引擎全新版本引爆互联网
Fluorescein-PEG-DSPE Phospholipid-Polyethylene Glycol-Fluorescein Fluorescent Phospholipid PEG Derivatives
Unity Text一个简单的输入特效
Use usb_cam to open multiple cameras at the same time
Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....
【Rhapsody学习笔记】2:Count Down
Detailed explanation of mysql transaction principle
windows下mysql忘记密码登录,并创建用户
Unity转微信小游戏与JS交互
Websocket协议解析与QT代码示例
UR3机器人运动学分析之正运动学分析
Cholesterol-PEG-DBCO Cholesterol-Polyethylene Glycol-Diphenylcyclooctyne Chemical Reagent
ROS subscription to multiple topics time synchronization problem