今天我收到一个任务,使用python-3计算文本中的“地球”一词。我确实知道我必须使用s.count
,但在文本中,这个字同时写入上寄存器和下寄存器。我发现s.lower().count("earth")
是我需要的,但我不明白为什么s.upper().count("earth")
或s.lower().count("Earth")
或s.upper().count("Earth")
不需要?我只需要一个解释。全文如下:
s = "In a distant, but not so unrealistic, future\
where mankind has abandoned earth because it has\
become covered with trash from products sold by\
the powerful multi-national Buy N Large corporation,\
WALLE, a garbage collecting robot has been left to\
clean up the mess. Mesmerized with trinkets of Earth's\
history and show tunes, WALLE is alone on Earth except\
for a sprightly pet cockroach. One day, EVE, a sleek\
(and dangerous) reconnaissance robot, is sent to Earth to\
find proof that life is once again sustainable."
当您使用
s.lower()
时,您的字符串变成了它:在本文中不存在EARTH,因为所有字符都是小写
另外,当您使用
s.uper()
时:所有字符都是上层的,不存在于地球上
调用
s.lower()
将使字符串全部使用小写字母。对于所有小写字母的字符串,您可以调用count("earth")
,因为字符串中没有大写字母相关问题 更多 >
编程相关推荐