Search code examples
pythoncapitalization

Keep uppercase after / in a string


I'm trying to title the following string:

"Men's L/S long sleeve"

I'm using string.capwords right now but it's not working as expected.

For example:

x = "Men's L/s Long Sleeve"
y = string.capwords(x)
print(y)

outputs:

Men's L/s Long Sleeve

but I want:

Men's L/S Long Sleeve (uppercased S after the /)

Is there an easy way to go about doing this?


Solution

  • Split by /, then run capwords individually on all the components, then rejoin the components

    text = "Men's L/s Long Sleeve"
    "/".join(map(string.capwords, text.split("/")))
    Out[10]: "Men's L/S Long Sleeve"