Hayley van Waas
Hayley van Waas

Reputation: 439

How to change the case of the last letter in a string in python?

So I want to change the last character in a string to lower case. The code below is the method I am using to print a string backwards, but list leaves the last character as a capital and I can't figure out how to fix this!

        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)

e.g: The input is:

Inbox

and this outputs:

XobnI

But I want it to output:

Xobni

Thanks in advance!

Upvotes: 1

Views: 1388

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250871

Use str.title:

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

Upvotes: 7

Related Questions