Skip to main content
 首页 » 编程设计

python之如何在python中更改字符串中最后一个字母的大小写

2025年05月04日22lvdongjie

所以我想将字符串中的最后一个字符更改为小写。下面的代码是我用来向后打印字符串的方法,但是列表将最后一个字符留为大写,我不知道如何解决这个问题!

        if s[-1] == "x": 
            new = "" 
            last_index = len(s)-1 
            for i in range(last_index, -1, -1): 
                new += s[i] 
            s = new 
            s = "".join(word[0].upper() + word[1:] for word in s.split()) 
        print (s) 

例如: 输入是:

Inbox 

这输出:

XobnI 

但我希望它输出:

Xobni 

提前致谢!

请您参考如下方法:

使用str.title:

>>> "Inbox"[::-1].title() 
'Xobni'