w3resource

Python: Find all lower and upper mixed case combinations of a given string


41. Mixed Case Combinations

Write a Python program to find all lower and upper mixed case combinations of a given string.

Sample Solution:

Python Code:

import itertools
def combination(str1):
    result = map(''.join, itertools.product(*((c.lower(), c.upper()) for c in str1)))
    return list(result)
st ="abc"
print("Original string:")
print(st)
print("All lower and upper mixed case combinations of the said string:")
print(combination(st))
st ="w3r"
print("\nOriginal string:")
print(st)
print("All lower and upper mixed case combinations of the said string:")
print(combination(st))
st ="Python"
print("\nOriginal string:")
print(st)
print("All lower and upper mixed case combinations of the said string:")
print(combination(st))

Sample Output:

Original string:
abc
All lower and upper mixed case combinations of the said string:
['abc', 'abC', 'aBc', 'aBC', 'Abc', 'AbC', 'ABc', 'ABC']

Original string:
w3r
All lower and upper mixed case combinations of the said string:
['w3r', 'w3R', 'w3r', 'w3R', 'W3r', 'W3R', 'W3r', 'W3R']

Original string:
Python
All lower and upper mixed case combinations of the said string:
['python', 'pythoN', 'pythOn', 'pythON', 'pytHon', 'pytHoN', 'pytHOn', 'pytHON', 'pyThon', 'pyThoN', 'pyThOn', 'pyThON', 'pyTHon', 'pyTHoN', 'pyTHOn', 'pyTHON', 'pYthon', 'pYthoN', 'pYthOn', 'pYthON', 'pYtHon', 'pYtHoN', 'pYtHOn', 'pYtHON', 'pYThon', 'pYThoN', 'pYThOn', 'pYThON', 'pYTHon', 'pYTHoN', 'pYTHOn', 'pYTHON', 'Python', 'PythoN', 'PythOn', 'PythON', 'PytHon', 'PytHoN', 'PytHOn', 'PytHON', 'PyThon', 'PyThoN', 'PyThOn', 'PyThON', 'PyTHon', 'PyTHoN', 'PyTHOn', 'PyTHON', 'PYthon', 'PYthoN', 'PYthOn', 'PYthON', 'PYtHon', 'PYtHoN', 'PYtHOn', 'PYtHON', 'PYThon', 'PYThoN', 'PYThOn', 'PYThON', 'PYTHon', 'PYTHoN', 'PYTHOn', 'PYTHON']

For more Practice: Solve these Related Problems:

  • Write a Python program to generate all possible combinations of a given string in which each character can be either upper or lower case.
  • Write a Python program to create an iterator that produces mixed-case variants of a string and then filter those that match a specific pattern.
  • Write a Python program to generate a list of all possible upper and lower case combinations of a given word using recursion and itertools.product.
  • Write a Python program to compute every mixed-case combination of a string and then sort the results by lexicographical order.

Go to:


Previous: Write a Python program to split a given list into specified sized chunks using itertools module.

Next: Write a Python program to create group of similar items of a given list.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.