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
Solution 1:
You can split the string on '/', use string.capwords on each substring, and rejoin on '/':
import string'/'.join(string.capwords(s) for s in"Men's L/S long sleeve".split('/'))
Solution 2:
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"
Solution 3:
It sounds like the /
might not be a reliable indicator that a capital letter is needed. In case the more reliable indicator is actually that the letter was capitalised originally, you could try iterating through each string and only extracting the letters got promoted to upper:
import string
x = "Men's L/S long sleeve"
y = string.capwords(x)
z = ''for i,l inenumerate(x):
if(l.isupper()):
z += l
else:
z += y[i]
So on strings like:
"brand XXX""shirt S/M/L""Upper/lower"
You'll get:
"Brand XXX""Shirt S/M/L""Upper/lower"
Which I think it probably more desirable than using the /
as an indicator.
Post a Comment for "Keep Uppercase After / In A String"