How to convert characters of a string from lowercase to upper case and vice versa without using string functions in python?

How to convert characters of a string from lowercase to upper case and vice versa without using string functions in python?

You can convert characters of a string from lowercase to uppercase and vice versa without using string functions in Python by leveraging the ASCII values of characters and performing arithmetic operations. Here's a simple example of how you can achieve this:

def convert_case(string): converted_string = '' for char in string: # Check if the character is a lowercase letter if 'a' <= char <= 'z': # Convert lowercase to uppercase by subtracting 32 from its ASCII value converted_char = chr(ord(char) - 32) # Check if the character is an uppercase letter elif 'A' <= char <= 'Z': # Convert uppercase to lowercase by adding 32 to its ASCII value converted_char = chr(ord(char) + 32) else: # Keep non-alphabetic characters unchanged converted_char = char converted_string += converted_char return converted_string # Example usage input_string = "Hello World!" converted_string = convert_case(input_string) print("Original:", input_string) print("Converted:", converted_string) 

In this example:

  • The convert_case function iterates through each character in the input string.
  • It checks if the character is a lowercase or uppercase letter using ASCII values.
  • If the character is a lowercase letter, it converts it to uppercase by subtracting 32 from its ASCII value.
  • If the character is an uppercase letter, it converts it to lowercase by adding 32 to its ASCII value.
  • Non-alphabetic characters remain unchanged.
  • The converted characters are concatenated to form the converted_string, which is then returned.

You can use this convert_case function to convert the case of characters in a string without using string functions like upper() or lower().

Examples

  1. Manual character case conversion in Python

    • Description: This query focuses on manually converting each character from lowercase to uppercase and vice versa.
    • Code:
      def manual_case_conversion(s): result = [] for char in s: if 'a' <= char <= 'z': result.append(chr(ord(char) - 32)) elif 'A' <= char <= 'Z': result.append(chr(ord(char) + 32)) else: result.append(char) return ''.join(result) print(manual_case_conversion("Hello World!")) # hELLO wORLD! 
  2. Python character ASCII manipulation for case conversion

    • Description: This query addresses how to use ASCII values to switch character cases.
    • Code:
      def ascii_case_conversion(s): converted = "" for char in s: ascii_val = ord(char) if 65 <= ascii_val <= 90: converted += chr(ascii_val + 32) elif 97 <= ascii_val <= 122: converted += chr(ascii_val - 32) else: converted += char return converted print(ascii_case_conversion("Python3.9")) # pYTHON3.9 
  3. Switching cases without string methods in Python

    • Description: This query focuses on implementing a function to switch cases without using built-in string methods.
    • Code:
      def switch_cases(s): result = [] for char in s: if 'a' <= char <= 'z': result.append(chr(ord(char) - 32)) elif 'A' <= char <= 'Z': result.append(chr(ord(char) + 32)) else: result.append(char) return ''.join(result) print(switch_cases("SwitchCase123")) # sWITCHcASE123 
  4. Converting lowercase to uppercase and vice versa using ASCII in Python

    • Description: This query focuses on using ASCII manipulation to convert characters.
    • Code:
      def convert_case(s): new_string = "" for char in s: if 'a' <= char <= 'z': new_string += chr(ord(char) - 32) elif 'A' <= char <= 'Z': new_string += chr(ord(char) + 32) else: new_string += char return new_string print(convert_case("Convert123")) # cONVERT123 
  5. Custom function for case conversion in Python without string functions

    • Description: This query is about creating a custom function to change character cases without using string methods.
    • Code:
      def custom_case_conversion(s): result = [] for char in s: if 'a' <= char <= 'z': result.append(chr(ord(char) - 32)) elif 'A' <= char <= 'Z': result.append(chr(ord(char) + 32)) else: result.append(char) return ''.join(result) print(custom_case_conversion("ExampleText")) # eXAMPLEtEXT 
  6. Python manual implementation of case conversion using loops

    • Description: This query focuses on using loops to manually implement case conversion.
    • Code:
      def manual_case_conversion_loop(s): new_str = "" for char in s: if 'a' <= char <= 'z': new_str += chr(ord(char) - 32) elif 'A' <= char <= 'Z': new_str += chr(ord(char) + 32) else: new_str += char return new_str print(manual_case_conversion_loop("ManualCase!")) # mANUALcASE! 
  7. ASCII-based case conversion in Python without using built-in functions

    • Description: This query covers converting case using ASCII values without built-in functions.
    • Code:
      def ascii_case_conversion_no_functions(s): result = [] for char in s: if 'a' <= char <= 'z': result.append(chr(ord(char) - 32)) elif 'A' <= char <= 'Z': result.append(chr(ord(char) + 32)) else: result.append(char) return ''.join(result) print(ascii_case_conversion_no_functions("Ascii123")) # aSCII123 
  8. Character by character case conversion in Python

    • Description: This query is about converting each character's case one by one.
    • Code:
      def char_by_char_case_conversion(s): new_s = [] for char in s: if 'a' <= char <= 'z': new_s.append(chr(ord(char) - 32)) elif 'A' <= char <= 'Z': new_s.append(chr(ord(char) + 32)) else: new_s.append(char) return ''.join(new_s) print(char_by_char_case_conversion("CharByChar")) # cHARbYcHAR 
  9. Python case conversion using list comprehension

    • Description: This query focuses on using list comprehension for case conversion.
    • Code:
      def case_conversion_list_comprehension(s): return ''.join([chr(ord(char) - 32) if 'a' <= char <= 'z' else chr(ord(char) + 32) if 'A' <= char <= 'Z' else char for char in s]) print(case_conversion_list_comprehension("ListComp")) # lISTcOMP 
  10. Manually switching cases in a Python string

    • Description: This query focuses on manually switching cases in a string.
    • Code:
      def manual_switch_case(s): result = [] for char in s: if 'a' <= char <= 'z': result.append(chr(ord(char) - 32)) elif 'A' <= char <= 'Z': result.append(chr(ord(char) + 32)) else: result.append(char) return ''.join(result) print(manual_switch_case("SwitchManually!")) # sWITCHmANUALLY! 

More Tags

rotativa linux-kernel script-task security ssl axes sharepoint-2013 reactjs sqlconnection expression

More Programming Questions

More Pregnancy Calculators

More Chemical thermodynamics Calculators

More Weather Calculators

More Retirement Calculators