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.
JavaScript
x
7
1
def uppercase(str_data):
2
ord('str_data')
3
str_data = str_data -32
4
chr('str_data')
5
return str_data
6
print(uppercase('abcd'))
7
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:
JavaScript
1
6
1
def uppercase(str_data):
2
return ''.join([chr(ord(char) - 32) for char in str_data if ord(char) >= 65])
3
4
print(uppercase('abcdé--#'))
5
>>> ABCDÉ
6
Without join:
JavaScript
1
9
1
def uppercase(str_data):
2
result = ''
3
for char in str_data:
4
if ord(char) >= 65:
5
result += chr(ord(char) - 32)
6
return result
7
print(uppercase('abcdé--#λ'))
8
>>> ABCDÉΛ
9