Python swapcase

The Python swapcase function swaps the case of the characters and returns a new string. Or say, swapcase converts the Lowercase letters to Uppercase and Uppercase characters to lowercase.

The swapcase Function keeps the Non-letters unchanged. The syntax of the Python swapcase function is

String_Value.swapcase()

Python swapcase method Example

The following set of examples helps to know about the swapcase Function

# Python SwapCase Method Example

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

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

# Performing Swapcase() function directly
Str3 = 'PYTHON ProGrammING'.swapcase()
print('Second Output of a SwapCase() method is = ', Str3)

Str4 = 'python1234TUTORIAL'.swapcase()
print('Third Output of a SwapCase() method is = ', Str4)

Str5 = '12345'.swapcase()
print('Fourth Output of a SwapCase() method is = ', Str5)
Python SwapCase

The following swapcase statement converts the lowercase letters to uppercase and uppercase letters into lowercase.

Str2 = Str1.swapcase()
print('First Output of a SwapCase() method is = ', Str2)

The swapcase function returns the output in a new string, instead of altering the original string.

print('Converted String is = ', Str1.swapcase())
print('Original String is = ', Str1)

From the above swap case String function screenshot, see that the original string Str1 is unchanged. To change the original string, write the following Python statement.

Str1 = Str1.swapcase()

The swapcase converts the lowercase to upper and vice versa. However, it leaves other values unchanged. Here, we used the numeric values along with letters.

Str4 = 'python1234TUTORIAL'.swapcase()
print('Third Output of a SwapCase() method is = ', Str4)

Str5 = '12345'.swapcase()
print('Fourth Output of a SwapCase() method is = ', Str5)