我不知道如何替换第一个字符的所有事件,不包括第一个字符而不影响最初的大小写。例如,我想把她变成海上最棒的,她在$ea上是$t。做这件事最有效的方法是什么
我尝试过使用.title()但没有成功,并且收到了错误字符大小写的拙劣输出
def change(s):
news=s.lower()
firstchar=news[0]
modifieds=news[1:].replace(firstchar,"$")
final=(firstchar+modifieds)
print(final.title())
change("She's The Best On The Sea")
她在$Ea上的$T是$T
以可读的方式:
输出:
将
re.sub
与re.IGNORECASE
一起使用:输出:
相关问题 更多 >
编程相关推荐