[{"element_type":"text","content":"\n  \n  \n  \n  \n        \u003cp\u003eHere's a short one...\u003c/p\u003e\n\u003cp\u003eSometimes I find myself wanting to pretty up a string. For example, a value might come in all in lower case letters and I want to capitalize the first letter or maybe go for capitalizing all first letters. Another use case is using variable names (conventionally all lower case) for captions.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e'this is my string'\u0026nbsp; ===\u0026gt;\u0026nbsp; 'This is my string'\u003c/li\u003e\n\u003cli\u003e'top values' ===\u0026gt; 'Top Values'\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003ePython has \u003cstrong\u003etitle()\u003c/strong\u003e and \u003cstrong\u003ecapitalize()\u003c/strong\u003e functions but these are not implemented in CircuitPython.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eAlso, strings may be in camel case that I want to fix - the Weather Powerup on AdafruitIO sends text data back this way:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e'mostlyClear' ===\u0026gt; 'Mostly Clear'\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eWhen there is a limited set of data I've used if/elif/else statements or used look-up dictionaries, but in some cases the inputs may not be known. Also, who wants to write all of the repetitive code!\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eSince CircuitPython does include upper() and lower() functions we can build functions to do this. So with the help of CoPilot I drafted and now include these three functions in my code when I am doing anything with strings:\u003c/p\u003e\n      \n\n\n\n","metadata":{}},{"element_type":"code","content":"def title_case(s):\n    if len(s) \u003e 3:\n        words = s.split()\n        titled = [word[0].upper() + word[1:].lower() if len(word) \u003e 0 else '' for word in words]\n        return ' '.join(titled)\n    else:\n        return s\n\ndef capitalize_case(s):\n    if len(s) \u003e 3:\n        return s[0].upper() + s[1:].lower()\n    else:\n        return s\n\ndef camel_to_title_case(s):\n    chars = []\n    for i, char in enumerate(s):\n        if char == '_':\n            chars.append(\" \")\n        elif char.isupper() and i != 0:\n            chars.extend([\" \", char])\n        else:\n            chars.append(char)\n    return ''.join(chars)","metadata":{"language":"python","linenums":false,"filename":""}},{"element_type":"text","content":"\n  \n  \n        \u003cp\u003eIn these sample functions I only apply to strings that are greater than three characters long as generally when I have short strings, I want them to keep the case (such as 'MPH'). But you can change this if you want it to make the change to all input strings. In the camel_to_title() function I also replace underscores with spaces, primarily for when I use variable names as captions.\u003c/p\u003e\n\u003cp\u003eHope you find this useful - and be sure to share any custom functions you can't live without.\u003c/p\u003e\n\u003cp\u003e\u0026nbsp;\u003c/p\u003e\n      \n\n","metadata":{}}]