How do I lowercase a string in Python?

后端 未结 5 1194
星月不相逢
星月不相逢 2020-11-22 12:57

Is there a way to convert a string from uppercase, or even part uppercase to lowercase?

For example, \"Kilometers\" → \"kilometers\".

5条回答
  •  长发绾君心
    2020-11-22 13:05

    Don't try this, totally un-recommend, don't do this:

    import string
    s='ABCD'
    print(''.join([string.ascii_lowercase[string.ascii_uppercase.index(i)] for i in s]))
    

    Output:

    abcd
    

    Since no one wrote it yet you can use swapcase (so uppercase letters will become lowercase, and vice versa) (and this one you should use in cases where i just mentioned (convert upper to lower, lower to upper)):

    s='ABCD'
    print(s.swapcase())
    

    Output:

    abcd
    

提交回复
热议问题