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 different thread here , I came up with this.
def uppercase(str_data): ord('str_data') str_data = str_data -32 chr('str_data') return str_data print(uppercase('abcd'))
However I am getting an error output: TypeError: ord() expected a character, but string of length 8 found.What am I missing here?
Advertisement
Answer
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ÉΛ