一、写一个简单的(±*/)的计算器,确保输入的都是数字(如果不是数字,让它重新输入)
while True
flag = 1
#flag = 1代表正常情况
input_data = input("请输入:")
if input_data == "exit":
print("正在退出")
break
result_tuple = ()
operator_list = ["+", "-", "*", "/"]
for operator in operator_list:
if operator in input_data:
result_tuple = input_data.partition(operator)
print(result_tuple)
if result_tuple:
if result_tuple[0].isdigit() and result_tuple[2].sidigit():
num1 = int(result_tuple[0])
num2 = int(result_tuple[2])
if result_tuple[1] == "+":
print(num1, "+", num2, "=", num1 + num2)
elif result_tuple[1] == "-":
print(num1, "-", num2, "=", num1 - num2)
elif result_tuple[1] == "/" and num2 != 0:
print(num1, "/", num2, "=", num1 / num2)
elif result_tuple[1] == "*":
print(num1, "*", num2, "=", num1 * num2)
else:
flag = 0
else:
flag = 0
if flag == 0:
print("输入有误,请重新输入!!!")
结果
二、while单层循环完成9 * 9乘法表
i = 1
while i <= 9:
j = 1
while(j <= i):
print(f'{i}*{j}={i*j}', end='\t')
j += 1
print('')
i += 1
三、str字符串中的strip, replace, split, partition, expandtabs, join center,ljust,rjust
strip()方法
用于移除字符串头尾指定的字符(默认为空格)。
str_data.strip([chars])
参数 chars – 移除字符串头尾指定的字符。
strip(self, chars=None, /)
Return a copy of the string with leading and trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
str_data = "00000i am cool guy00000"
print(str_data.strip('0'))
结果
i am cool guy
replace()
语法:replace(self, old, new, count=-1, /)
Return a copy with all occurrences of substring old replaced by new.
count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
If the optional argument count is given, only the first count occurrences are
replaced.
把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次
参数
old – 将被替换的子字符串。
new – 新字符串,用于替换old子字符串
max – 可选字符串, 替换不超过 max 次
返回值
返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。
split () 分割 和 join()合并
split()分割
split()可以基于指定分隔符将字符串分隔成多个子字符串(存储到列表中)。如果不指定分隔
符,则默认使用空白字符(换行符/空格/制表符)。
示例代码:
data = "you are yourself"
data.split('you')
result = data.split('you')
print(result)
结果
['', ' are ', 'rself']
join()合并
join0的作用和split()作用刚好相反,用于将一 系列子字符串连接起来。拼接字符串要点:
使用字符串拼接符+ ,会生成新的字符串对象,因此不推荐使用+来拼接字符串。推荐
使用join函数,因为join函数在拼接字符串之前会计算所有字符串的长度, 然后逐-拷贝,
仅新建一对象。
示例代码:
data = ['The', 'Chosen', 'One']
'\t'.join(data)
print('\t'.join(data))
结果
The Chosen One
partition()
partition根据指定的分隔符(sep)将字符串进行分割。从字符串左边开始索引分隔符sep,索引到则停止索引。
示例代码:
str_data = "*hello*you*"
result = str_data.partition('*')
print(result)
结果
('', '*', 'hello*you*')
expandtabs
expandtabs将字符串中的 \t 替换为一定数量的空格。默认N=8
示例代码
str_data = "*hello\tworld*"
print(str_data)
result = str_data.expandtabs(tabsize=9)
print(result)
结果:
*hello world*
*hello world*
center
center返回一个居中的新字符串,可以自行设置宽度和填充字符
示例代码:
str_data = "zmm"
result = str_data.center(4, 'c')
print(result)
result1 = str_data.center(6, 'z')
print(result1)
结果:
zmmc zzmmz
ljust
ljust返回一个原字符串左对齐,并使用fillchar填充(默认为空格)至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
示例代码:
str_data = "123"
result = str_data,ljust(5, '*')
print(result)
结果:
123**
rjust
rjust返回一个原字符串右对齐,并使用fillchar填充(默认为空格)至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
示例代码:
str_data = "123"
result = str_data.rjust(5, "*")
print(result)
结果:
**123
格式化输出
例:
姓名 年龄 性别 家庭住址
xxx xxxx
list_data = [{name: xxx, age: xxx, gender: xxx, address}, .....]
包含居中对齐,向左对齐, 向右对齐
list_data = [{"name": "chen", "age": "20", "gender": "男", "address": "福州"}]
print("我的名字是{[name]:*^6}".format(list_data[0]))
print("今年{[age]:*<5}岁".format(list_data[0]))
print("我是{[gender]:*>5}".format(list_data[0]))
print("我住在{[address]}".format(list_data[0]))
结果:
我的名字是*chen*
今年20***岁
我是****男
我住在福州