-1

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?

uzhy
  • 95
  • 2
  • 4
  • 14
  • We need to know more about the constraints. Can the input string only contain lower case alphabetic characters? – timgeb Apr 22 '17 at 10:11
  • 1
    Also, "I am not getting any output" seems unlikely. The output should be `TypeError: ord() expected a character, but string of length 4 found`. – timgeb Apr 22 '17 at 10:12
  • Also how are `ord` and `chr` (which you misspelled) not inbuilt functions? – timgeb Apr 22 '17 at 10:14
  • My apologies, I have rectified the misspelled chr. I am allowed to use chr() and ord() functions. – uzhy Apr 22 '17 at 10:18
  • 1
    On the one hand, I understand the rationale of teachers restricting the use of various built-in functions and methods when they are trying to teach particular programming concepts. OTOH, the resulting code is often rather un-Pythonic: Python is famous for coming with "batteries included", and a Python course should teach you how to use those batteries, not forbid you from using them. – PM 2Ring Apr 22 '17 at 10:59
  • 1
    @PM2Ring I agree. I think this assignment would be improved by having a second part, to the tune of "and then verify that your solution produces the same output as the built-in str.upper() function, on the following three inputs..." – Bryant Feb 28 '20 at 15:20

7 Answers7

2

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ÉΛ
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • Does not work for special characters like `;` or `/` – Mr. Xcoder Apr 22 '17 at 10:30
  • Thanks for your response. Can you suggest without the join function? I am not allowed to use it. Also, @Mr -Xcoder, I can pass of special characters such as ; or / as exceptions. Using only alphabets for my case. Simple words – uzhy Apr 22 '17 at 10:47
  • Now it is better, it does not use `index()` as well and now that it meets the requirements it's a really good answer. – Mr. Xcoder Apr 22 '17 at 10:52
1

The best way, in my opinion is using a helper string, representing the alphabet, if you do not want to use chr() and ord():

def toUppercase(s):
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    result = ''
    for x in s:
        if x not in alphabet or alphabet.index(x)>=26:
            result += x
        else:
            result += alphabet[alphabet.index(x)+26]
    return result

This also handles punctuation such as ; or ..


Update:

As per the OP's request, this is a version without index():

def toUppercase(s):
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    result = ''
    for x in s:
        for pos in range(52):
            if alphabet[pos] == x:
                i = pos
        if x not in alphabet or i>=26:
            result += x
        else:
            result += alphabet[i+26]
    return result

print(toUppercase('abcdj;shjgh'))
Community
  • 1
  • 1
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
1

Here is a program to convert the string to uppercase without using inbuilt functions:

Str1=input("Enter the string to be converted uppercase: ")

for i in range (0,len(Str1)):

   x=ord(Str1[i])
   if x>=97 and x<=122:
       x=x-32
   y=chr(x)
   print(y,end="")
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Parav Kaushal
  • 101
  • 1
  • 5
0

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.

Aditya Lahiri
  • 447
  • 3
  • 4
0

The below-simplified code help to convert Lower-case alphabets to Upper-case alphabets using a simple calculation

code :

def toUppercase(string):
    convertedCharacter = ''
    for i in string: 
         convertCharacter += chr( ( (ord(i)) -32) ) 
    return convertCharacter
0
char=input("Enter lowercase word :")
for letter in char:
    s=ord(letter)
    if s>=97 and s<=122:
        print(chr(s-32),end=" ")
0
def uppercase(str_data):
   ord('str_data')
   str_data = str_data -32
   chr('str_data')
   return str_data
print(uppercase('abcd'))

in this code ord takes single character as an argument but you have given more than one that's why it's showing an error. Take single character at a time convert it to upper case and make a single string like below.

def convert_to_lower(string):
    new="" 
    for i in string:
       j=ord(i)-32  #we are taking the ascii value because the length of lower 
             #case to uppercase is 32 so we are subtracting 32

       if 97<=ord(i)<=122 :  #here we are checking whether the charecter is lower
                      # case or not if lowercase then only we are converting into
                      #uppercase
           new=new+chr(j)
       else:  #if the character is not the lowercase alplhaber we are taking as it is
           new=new+i
    print(new)

convert_to_lower("hello world")