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.

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.

On each iteration, apply the swapcase() function to convert lowercase characters to uppercase, vice versa.

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.

  • The for loop iterates over the characters in a given string from start to end. On each iteration, the if-else statement is implemented.
  • On every iteration (new character from a string), the isupper() in the if statement checks whether the character is uppercase. If true, the lower() function converts it to the lowercase.
  • Else, the upper() function converts the character to uppercase. Also, check the title() 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 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