From 9c9bb29104893c142ce4148a27832457db4d5509 Mon Sep 17 00:00:00 2001 From: Ponnuri Pavan Kumar Date: Tue, 25 Nov 2025 20:22:44 +0530 Subject: [PATCH 1/2] Improve capitalize(): add Unicode support and handle leading spaces --- strings/capitalize.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/strings/capitalize.py b/strings/capitalize.py index 628ebffc8852..21f032779acd 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -1,27 +1,27 @@ def capitalize(sentence: str) -> str: """ - Capitalizes the first letter of a sentence or word. + Capitalizes the first alphabetical character of a sentence. + Examples: >>> capitalize("hello world") 'Hello world' - >>> capitalize("123 hello world") - '123 hello world' - >>> capitalize(" hello world") - ' hello world' - >>> capitalize("a") - 'A' + >>> capitalize(" hello world") + ' Hello world' + >>> capitalize("123 abc") + '123 abc' + >>> capitalize("ñandú bird") + 'Ñandú bird' >>> capitalize("") '' """ + if not sentence: return "" - # Capitalize the first character if it's a lowercase letter - # Concatenate the capitalized character with the rest of the string - return sentence[0].upper() + sentence[1:] - - -if __name__ == "__main__": - from doctest import testmod + # Find the first alphabetic character and capitalize it + for idx, char in enumerate(sentence): + if char.isalpha(): + return sentence[:idx] + char.upper() + sentence[idx+1:] - testmod() + # If no alphabetic character exists, return the original string + return sentence From 41cc2b8ee7b94faa1d6aed6ae996ed46f07a69ff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 25 Nov 2025 14:55:53 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/capitalize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/capitalize.py b/strings/capitalize.py index 21f032779acd..4759e88a84db 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -21,7 +21,7 @@ def capitalize(sentence: str) -> str: # Find the first alphabetic character and capitalize it for idx, char in enumerate(sentence): if char.isalpha(): - return sentence[:idx] + char.upper() + sentence[idx+1:] + return sentence[:idx] + char.upper() + sentence[idx + 1 :] # If no alphabetic character exists, return the original string return sentence