Find all upper, lower and mixed case combinations of a string

后端 未结 6 1330
名媛妹妹
名媛妹妹 2020-11-29 04:08

I want to write a program that would take a string, let\'s say \"Fox\", then it would display:

fox, F         


        
6条回答
  •  萌比男神i
    2020-11-29 04:40

    one liner using list comprehension:

    from itertools import  permutations
    strs='fox'
    combin=[''.join(x)for x in  permutations(list(strs)+list(strs.upper()),3) if ''.join(x).lower()=='fox']
    print(combin)
    ['fox', 'foX', 'fOx', 'fOX', 'Fox', 'FoX', 'FOx', 'FOX']
    

    using for-in loop:

    from itertools import  permutations
    strs='fox'
    lis2=list(strs)+list(strs.upper())
    for x in  permutations(lis2,3):
        if ''.join(x).lower()=='fox':
            print(''.join(x))
    
    fox
    foX
    fOx
    fOX
    Fox
    FoX
    FOx
    FOX
    

提交回复
热议问题