[Python] Case insensitive dictionary

Discussão em 'Python' iniciado por Stack, Agosto 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like my dictionary to be case insensitive:

    text = "practice changing the color"

    words = {'color': 'colour',
    'practice': 'practise'}

    def replace(words,text):

    keys = words.keys()

    for i in keys:
    text= text.replace(i ,words)
    return text

    text = replace(words,text)

    print text


    Output = practise changing the colour

    I'd like another string, "practice changing the Color", (where Color starts with a capital) to give the same output.

    I believe there is a general way to convert to lowercase using mydictionary[key.lower()] but I'm not sure how to best integrate this into my existing code.

    Continue reading...

Compartilhe esta Página