s.lower()计数(“接地”)和s.upper()计数(“接地”)之间有什么区别?

2024-06-02 06:08:46 发布

您现在位置:Python中文网/ 问答频道 /正文

今天我收到一个任务,使用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."

Tags: andtheto文本iscountwithrobot
2条回答

当您使用s.lower()时,您的字符串变成了它:

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.

在本文中不存在EARTH,因为所有字符都是小写

另外,当您使用s.uper()时:

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()将使字符串全部使用小写字母。对于所有小写字母的字符串,您可以调用count("earth"),因为字符串中没有大写字母

相关问题 更多 >