Skip to content

🐍 Program 141: ASCII Capitalization #164

@iamAntimPal

Description

@iamAntimPal

🐍 Program 141: ASCII Capitalization

📌 Description

Create a function that takes a string as input and capitalizes a letter if its ASCII code is even and returns its lowercase version if its ASCII code is odd.


💡 Code Example

def ascii_capitalize(txt):
    return ''.join([char.upper() if ord(char) % 2 == 0 else char.lower() for char in txt])

# Examples
print(ascii_capitalize("to be or not to be!"))  # ➞ "To Be oR NoT To Be!"
print(ascii_capitalize("THE LITTLE MERMAID"))  # ➞ "THe LiTTLe meRmaiD"
print(ascii_capitalize("Oh what a beautiful morning."))  # ➞ "oH wHaT a BeauTiFuL moRNiNg."

✅ Output

To Be oR NoT To Be!
THe LiTTLe meRmaiD
oH wHaT a BeauTiFuL moRNiNg.

🧠 Explanation

  • The function iterates over each character in the input string.
  • It checks the ASCII value of each character using ord(char).
  • If the ASCII value is even, it capitalizes the character; if the ASCII value is odd, it returns the character in lowercase.

Metadata

Metadata

Assignees

Labels

PythonPython Interview QuestionsenhancementNew feature or requesthelp wantedExtra attention is neededquestionFurther information is requested

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions