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?
The best way, in my opinion is using a helper string, representing the alphabet, if you do not want to use
chr()
andord()
:This also handles punctuation such as
;
or.
.Update:
As per the OP's request, this is a version without
index()
:Program to
convert
the string to uppercase without usinginbuilt
function
ord()- Return the Unicode code point for a one-character string.
You have to send a one character string as an argument. Here, you are sending the string 'abcd' which has 4 characters which is causing the issue. Send each character separately to the function and thus do 4 calls to the function to get the result.
You need to execute ord() for each character of your input string. instead of the input string:
Without join: