Question: I'm trying to title the following string:

`"Men's L/s button-up"`

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

For example:

```
x = "Men's L/s button-up"
y = string.capwords(x)
print(y)

```

outputs:

`Men's L/s Button-up`

but I want:

`Men's L/S Button-Up` (uppercased S after the / and uppercased U after the -)

More generally, how do I ensure that all letters following a space or punctuation are capitalized? 

This is a follow-up from: 

[Keep uppercase after / in a string](https://stackoverflow.com/questions/56411174/keep-uppercase-after-in-a-string)

Answer: 
The `string.capwords` won't work for this, because internally, it uses `str.split()` to generate tokens ([docs](https://docs.python.org/3.5/library/string.html#string.capwords)). `str.split()` can only split on runs of whitespace (default behavior) or using a user-specified character/string ([docs](https://docs.python.org/3.5/library/stdtypes.html#str.split)). If a string is specified, it is considered as a whole, and not as separated characters.

To achieve what you want, I would use regular expressions, for example:

```
import re

x = "Men's L/s button-up"
y = re.sub(r'[\w]+', lambda m: m.group(0).capitalize(), x)
print(y)
# "Men'S L/S Button-Up"

```

`re.sub` ([docs](https://docs.python.org/3/library/re.html#re.sub)) can be used to replace substrings inside the given string. The second argument there is a function that takes a [match object](https://docs.python.org/3/library/re.html#match-objects) argument and returns the replacement string (in this case, we capitalize the full match).

If you stumble across this question, also make sure to look at @Christian answer just below this, as it provides a nice alternative way to split the string :)
The way I would approach this is by splitting up each word you want to capitalize (using `re.split`), capitalize said word (using `str.capitalize`), and then recombine the words to get a single string (using `str.join`): 

```
>>> from re import split
>>> 
>>> string = "Men's L/s button-up"
>>> ''.join([word.capitalize() for word in split("([ /-])", string)])
"Men's L/S Button-Up"
>>

```

If you need to split on more punctuation characters, simply add them to the regular expression. For example, if you do decide you need to capitalize words/characters after apostrophes, adding:

```
"([ '/-])"

```

to the regular expression would work:

```
>>> ''.join([word.capitalize() for word in split("([ '/-])", string)])
"Men'S L/S Button-Up"
>>>

```