Python String Case Conversion: upper(), lower(), and title()
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
Python strings are immutable, so upper(), lower(), and title() do not modify the original string; they return a new string with the requested case conversion. upper() converts every cased character to uppercase, while lower() converts every cased character to lowercase. title() returns a title-cased version: the first alphabetic character of each word becomes uppercase and the remaining characters in that word become lowercase. Note that Python's definition of a word boundary is not limited to spaces, so punctuation such as apostrophes can create new words (for example, they're.title() becomes They'Re).
The example function accepts any string and returns a dictionary containing the original value plus the results of all three methods. Running the script prints several inputs and outputs, including mixed-case text, numbers, and apostrophes. These methods work on Unicode characters and leave uncased characters such as digits and punctuation unchanged. Each method scans the entire string once, so the time complexity is O(n), where n is the number of characters. Space complexity is O(n) because a new string is created for each conversion. They are ideal for simple, locale-independent case normalization; use casefold() for aggressive case-insensitive comparisons.