Python 如何在使用.lower()后恢复为大小写混合的单词?

Python 如何在使用.lower()后恢复为大小写混合的单词?,python,Python,需要使用.lower()函数将“word”转换为所有小写,因为代码不需要区分大小写。但是,现在我需要它恢复到混合大小写的原始单词,以便执行统计大写和小写字符的功能(我已经完成了)。您必须跟踪旧单词才能恢复它: word = 'MixedCase' old_word = word # old_word -> 'MixedCase' word = word.lower() # word -> 'mixedcase' # restore: word

需要使用
.lower()
函数将“word”转换为所有小写,因为代码不需要区分大小写。但是,现在我需要它恢复到混合大小写的原始单词,以便执行统计大写和小写字符的功能(我已经完成了)。

您必须跟踪旧单词才能恢复它:

word = 'MixedCase'
old_word = word             # old_word -> 'MixedCase'
word = word.lower()         # word -> 'mixedcase'

# restore:
word = old_word             # word -> 'MixedCase'

您不能,因为您不再知道哪些不是大写。您需要两个名称,一个是原始字符串,另一个是
.lower()
字符串。只需将原始名称分配给其他变量即可