Search code examples
pythonfilecapitalize

How to capitalize sentences when middle of the sentence is enclosed in parenthesis like (unix)?


Suppose i have string like S="python (unix)" and i want to get following output : Python (Unix). Please suggest me.

How to solve above when senetence is to read from text file in Python?


Solution

  • Here is one approach, using re.sub with a callback function which capitalizes each matching word:

    S = "python (unix)"
    out = re.sub(r'\b\w+\b', lambda m: m.group().capitalize(), S)
    print(out)
    

    This prints:

    Python (Unix)