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 is

String_Value.swapcase()

Python swapcase method Example

The following set of examples helps to know about Python 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 Python 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 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 Example