python - 查找字符串的所有大写、小写和混合大小写组合

标签 python string

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

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']

关于python - 查找字符串的所有大写、小写和混合大小写组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11144389/

相关文章:

python - "Unsorting"快速排序

python - 如何根据模型领域选择形式

c - linux/windows 上的宽字符字符串函数

C 字符串数组 sizeof() 更改

java - 使用一组有限值的 String 类属性的最有效方法

python - 如何在类中调用函数?

python - 如何获取行的 nr 直到下一个值

javascript - 在 javascript 中编码了 unicode 字符串后,如何在 Python 中对其进行解码?

连接字符串中的 Javascript 构造

c++ - 无法将参数从 `const char *` 转换为 `char *`