Skip to content

Commit 9cda4c3

Browse files
walterclaude
authored andcommitted
python5/Example.md:.format() 改写为 f-string,并同步更新讲解注释
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 89cdf1b commit 9cda4c3

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

Article/PythonBasis/python5/Example.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
# 打印九九乘法表
99
for i in range(1, 10):
1010
for j in range(1, i+1):
11-
# 打印语句中,大括号及其里面的字符 (称作格式化字段) 将会被 .format() 中的参数替换,注意有个点的
12-
print('{}x{}={}\t'.format(i, j, i*j), end='')
11+
# 使用 f-string 直接在字符串前加 f,大括号里写表达式即可被求值并插入到字符串中
12+
print(f'{i}x{j}={i*j}\t', end='')
1313
print()
1414
```
1515

@@ -36,9 +36,9 @@ for i in range(1, 10):
3636

3737
year = int(input("请输入一个年份: "))
3838
if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
39-
print('{0} 是闰年' .format(year))
39+
print(f'{year} 是闰年')
4040
else:
41-
print('{0} 不是闰年' .format(year))
41+
print(f'{year} 不是闰年')
4242

4343
```
4444

0 commit comments

Comments
 (0)