Python swapcase

The Python swapcase function swaps the case of the characters and returns a new string. Or say, it converts the Lowercase letters to Uppercase and vice versa. It keeps the Non letters unchanged, and the syntax of the swap case is

String_Value.swapcase()

The swapcase() function does not modify the original string. Instead, it returns a completely new string by converting uppercase letters to lowercase and lowercase characters to uppercase.

Python swapcase method Example

The following set of examples helps to know about swapcase. First, we declared Str1 with a mixed text. And the first statement converts the lowercase letters to uppercase and uppercase letters into lowercase.

The swapcase function returns the output in a new string instead of altering the original. We used the next two lines of code to show the difference. Please refer to the Python string article and the list of available Python string functions from our Learn Python page.

From the below String method screenshot, see that the original string Str1 is unchanged. To change the original string, write the following Python swapcase statement.

Str1 = Str1.swapcase()

It only converts the lowercase to the upper and vice versa. However, it leaves other values unchanged. In the following two statements, we used numeric values along with letters.

Str1 = 'Learn PYTHON AT TutOrial GateWay'
Str2 = Str1.swapcase()
print('First Output is = ', Str2)

# Observe the Original Text
print('Converted is = ', Str1.swapcase())
print('Original is = ', Str1)

Str3 = 'PYTHON ProGrammING'.swapcase()
print('Second Output of method is = ', Str3)

Str4 = 'python1234TUTORIAL'.swapcase()
print('Third Output is = ', Str4)

Str5 = '12345'.swapcase()
print('Fourth Output is = ', Str5)
Python swapcase Function Example

Python swapcase list of strings

To use the built-in swapcase() function on a list of strings, we must use a list comprehension or a map function. We cannot directly use swapcase() on lists; instead, we use a list comprehension to iterate over the list of strings. Please refer to the Python list comprehension and Python map function.

On each iteration, apply the swapcase() function to convert lowercase characters to uppercase, vice versa. Explore more about the Python lists.

words = ["UN", "usa", "world", "unsc"]

result = [word.swapcase() for word in words]
print(result)
['un', 'USA', 'WORLD', 'UNSC']

How to swap uppercase and lowercase characters without using swapcase in Python?

We can use the combination of upper(), lower(), isupper(), and islower() functions inside the join() method to swap cases of string characters without using the swapcase() function.

text = "new YEAR"
swapped = "".join(c.lower() if c.isupper() else c.upper() for c in text)
print(swapped)
NEW year

If the task is not to use any built-in function to swap uppercase and lowercase characters, we must use the technique below. Here, we used the Python elif statement inside the for loop to check whether each character is between A and Z or a to z.  If the character is uppercase, add 32 to its ASCII value. If the character is lowercase, subtract 32 from its ASCII value.

text = "10th Wall STReet"

result = ""

for ch in text:
if 'A' <= ch <= 'Z':
result += chr(ord(ch) + 32)
elif 'a' <= ch <= 'z':
result += chr(ord(ch) - 32)
else:
result += ch

print("Swapped case:", result)
Swapped case: 10TH wALL strEET