1.self
i = 1
while i<=9:j = 1while j<= i:print("j * i =",end='')print(j * i ,end='')print(" ",end='')j += 1i +=1print()
实现结果:
2.改进
i = 1
while i<=9:j = 1while j<= i:# print("j * i =",end='')# print(j * i ,end='')# print(" ",end='')print(f"{j} * {i} = {j * i}\t",end='')j += 1i +=1print()
实现结果:
3.反思
(1): 禁用自动换行:
要禁用`print()`函数的自动换行功能,可以向其传递`end`参数,并将其设置为空字符串或所需的结束字符。
print("Hello,", end=" ")
print("World!")
输出结果:
Hello, World!
(2):“\t”制表符的使用
制表符常用于对齐文本。例如,你可以使用制表符将一组数字对齐到小数点后两位:
numbers = [12.345, 67.89, 4.5678]
formatted_numbers = "\t".join("{:.2f}".format(num) for num in numbers)
print(formatted_numbers)
```
输出:12.35 67.89 4.57