Part of the series

Several example codes

~2 min read • Updated Sep 23, 2025

Program Overview

This Python program reads a string and converts all lowercase letters to uppercase and all uppercase letters to lowercase.
The charChange() function iterates through each character in the string and builds a new string with the converted characters.
The final result is returned and displayed.


Python Code:


def charChange(str):
    str1 = ""
    for ch in str:
        if ch.islower():
            str1 += ch.upper()
        elif ch.isupper():
            str1 += ch.lower()
        else:
            str1 += ch
    return str1

# Get input from user
input_text = input("Enter a string: ")
converted = charChange(input_text)
print("Converted string:", converted)

Sample Output:


Enter a string: Hello World!  
Converted string: hELLO wORLD!

Explanation:

- The charChange() function uses a for loop to examine each character
- If the character is lowercase, it’s converted to uppercase using upper()
- If the character is uppercase, it’s converted to lowercase using lower()
- Non-letter characters (spaces, punctuation) are added unchanged
- The final string is returned and printed


Written & researched by Dr. Shahin Siami