Search code examples
functioncaseuppercase

Upper case a full string without using .upper function? PYTHON


I was wondering how I could completely upper case a string without the use of any functions involving .upper()? Is there a way to possibly capitalize with the ord() function? I am in an exercise where I must refrain from using the .upper function.


Solution

  • ord("A") --> 65                  ord("B") --> 66            ...
    ord("a") --> 97                  ord("b") --> 98            ...
    

    So whenever you want to change lowercase character to uppercase without using upper() function, you can just find the ASCII value of lowercase using ord() function, subtract 32 (which is the difference between uppercase and lowercase) and then use chr() function to convert it back to char.