I have the following procedure:
def capitalize(self, text):
t = ' '.join([ ''.join([w[0].upper()]+[w[1:]]) for w in text.split()])
if text and text[-1] == ' ':
t = ''.join([t] + [' '])
return t
It takes a string text. What it's supposed to do:
- Capitalize first letter of each string group (word) coming after a space and preserve the space in the end of the text if there was any supplied.
ex:
'home swe eeeet home' -> 'Home Swe Eeeet Home'
'heLLo OoO ooo ' -> 'HeLLo OoO Ooo ' (space preserved in the end)
Question:
With my limited, totally non - expert level of Python, I tried to create this procedure as memory efficient and as fast as possible.
Is the approach of converting things into list and joining them to not to keep creating a new string efficient in this case?
Is there a better, more pythonic way to achieve this?
Furthermore:
This procedure is called each time a key is pressed onto a text field on a GUI application.