Broad Network


4.4 Toggle the Case of Each Character in a String in Python

4. Basic String Algorithm Problems in Python

Full Course on Data Structures and Algorithms in Python

By: Chrysanthus Date Published: 3 Feb 2026

The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.

You are given a string. You are asked to toggle the case of each character: A) given that the string consists of ASCII characters, and B) the character set/code is not known.

You are asked to use O(N) time and O(N) space for each problem.

Example of Toggling Character Case

Input : s = "Broad Network Corporation"
Output : "bROAD nETWORK cORPORATION"

A) The string consists of ASCII Characters

In the ASCII Code Table, if 32 is added to the code of an uppercase letter, the lowercase code is obtained; if 32 is subtracted from the code of a lowercase letter, the uppercase code is obtained. With that, just iterate over the given string. The program is (read through the code and comments):

def toggleChars(str):
    N = len(str);    #length of string
    
    strlist = list(str)    # for mutation since string content is constant
    
    #Conversion according to ASCII values
    for i in range(N):
        if (strlist[i] >= 'a' and strlist[i] <= 'z'):
            # Convert lowercase to uppercase
            strlist[i] = chr((ord(strlist[i]) - 32));
        elif (strlist[i] >= 'A' and strlist[i] <= 'Z'):
            # Convert uppercase to lowercase
            strlist[i] = chr((ord(strlist[i]) + 32));
    
    # Join list back into a string
    str = "".join(strlist)
    return str

#main area
stri = "Broad Network Corporation";    #given string
sr = toggleChars(stri);
print(sr);  

The output is:

    b R O A D   n E T W O R K   c O R P O R A T I O N 

as expected. The time complexity is actually O(2N), with O(N) for finding the length of the input string array and O(N) for iterating over the string array in the called function. However, the coefficient (multiplier of 2 or 3 or 1/2, etc.) is normally omitted. The space complexity is O(N), since the input string array and the string array in the called function are the same array.

B) The Character Set and its Coding is not known

When the character set and its corresponding coding are not known, import the ctype.h library which has the isupper(), tolower() and toupper() predefined functions. Each of these functions operate in approximately O(1) time, which is ignored when quoting the time complexity. Any temporary space used by these predefined functions are also ignored when quoting the space complexity. The following program illustrates the use of these functions to toggle the case of characters in a string (read through the code and comments):

def toggleChars(str):
    N = len(str);    #length of string
    
    strlist = list(str)    # for mutation since string content is constant
    
    #Iterate over the same string
    for i in range(0, N): 
        # Check the case of the character and
        # toggle accordingly
        if strlist[i].isupper():
            strlist[i] = strlist[i].lower();
        else:
            strlist[i] = strlist[i].upper();
    
    # Join list back into a string
    str = "".join(strlist)
    return str

#main area
stri = "Broad Network Corporation";    #given string
sr = toggleChars(stri);
print(sr);  

The output is:

    b R O A D   n E T W O R K   c O R P O R A T I O N 

as expected. The time complexity is officially O(N) and the space complexity is officially O(N)

Thanks for reading.





Related Links

More Related Links

Cousins

BACK NEXT

Comments


Note: You can use the Search Box above to find articles and discussions of interest.