mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
capitalize (#2389)
* Create capitalize.py This function will capitalize the first character of a sentence or a word * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update strings/capitalize.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update capitalize.py * Update strings/capitalize.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update capitalize.py * Update capitalize.py * Update capitalize.py * Update strings/capitalize.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update capitalize.py * Update strings/capitalize.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update capitalize.py * Update capitalize.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
a1d1a44f51
commit
c38dec091f
27
strings/capitalize.py
Normal file
27
strings/capitalize.py
Normal file
@ -0,0 +1,27 @@
|
||||
from string import ascii_lowercase, ascii_uppercase
|
||||
|
||||
|
||||
def capitalize(sentence: str) -> str:
|
||||
"""
|
||||
This function will capitalize the first letter of a sentence or a word
|
||||
>>> capitalize("hello world")
|
||||
'Hello world'
|
||||
>>> capitalize("123 hello world")
|
||||
'123 hello world'
|
||||
>>> capitalize(" hello world")
|
||||
' hello world'
|
||||
>>> capitalize("a")
|
||||
'A'
|
||||
>>> capitalize("")
|
||||
''
|
||||
"""
|
||||
if not sentence:
|
||||
return ''
|
||||
lower_to_upper = {lc: uc for lc, uc in zip(ascii_lowercase, ascii_uppercase)}
|
||||
return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctest import testmod
|
||||
|
||||
testmod()
|
Loading…
Reference in New Issue
Block a user