Conversion of string to upper case without inbuilt methods

前端 未结 7 1698
轻奢々
轻奢々 2021-01-29 08:28

I am trying to perform conversion from a lowercase to uppercase on a string without using any inbuilt functions (other than ord() and char()). Following the logic presented on a

7条回答
  •  太阳男子
    2021-01-29 08:49

    You need to execute ord() for each character of your input string. instead of the input string:

    def uppercase(str_data):
        return ''.join([chr(ord(char) - 32) for char in str_data if ord(char) >= 65])
    
    print(uppercase('abcdé--#'))
    >>> ABCDÉ
    

    Without join:

    def uppercase(str_data):
        result = ''
        for char in str_data:
            if ord(char) >= 65:
                result += chr(ord(char) - 32)
        return result
    print(uppercase('abcdé--#λ'))
    >>> ABCDÉΛ
    

提交回复
热议问题