设有一个英文文本文件,读取其内容,并把其中的大写字母变成小写字母,小写字母变成大写字母。

设有一个英文文本文件,读取其内容,并把其中的大写字母变成小写字母,小写字母变成大写字母。

with open('test.txt','r') as f:
    for i in f.read():
        if i.islower():
            i = chr(ord(i)-32)
        elif i.isupper():
            i = chr(ord(i)+32)
        print(i,end='')
'''
原内容:
Hello
World!
Hi!
Computer
Good afternoon
Bye
'''
with open(filename) as f:
    print(f.read().swapcase())
'''
--result
hELLO
wORLD!
hI!
cOMPUTER
gOOD AFTERNOON
bYE

'''