查找字符串的所有大写、小写和混合大小写组合 [英] Find all upper, lower and mixed case combinations of a string

查看:46
本文介绍了查找字符串的所有大写、小写和混合大小写组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个接受字符串的程序,比如说"Fox",然后它会显示:

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

fox, Fox, fOx, foX, FOx, FoX, fOX, FOX

到目前为止我的代码:

string = raw_input("Enter String: ")
length = len(string)
for i in range(0, length):
    for j in range(0, length):
        if i == j:
            x = string.replace(string[i], string[i].upper())
            print x

到目前为止的输出:

Enter String: fox
Fox
fOx
foX
>>> 

推荐答案

import itertools

s = 'Fox'
map(''.join, itertools.product(*zip(s.upper(), s.lower())))
>>> ['FOX', 'FOx', 'FoX', 'Fox', 'fOX', 'fOx', 'foX', 'fox']

这篇关于查找字符串的所有大写、小写和混合大小写组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆