New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

NumPy swapcase() function

In this tutorial, we will cover swapcase() function of the char module in Numpy library.

The swapcase() function is mainly used to return an element-wise copy of the string with the uppercase characters of the string converted to lowercase and lowercase characters converted to uppercase. In short, it swaps or changes the case of the characters in a given string.

This function is locale-dependent for an 8-bit string.

Syntax of swapcase():

The syntax required to use this function is as follows:


The above syntax indicates that capitalize() function takes two parameters.

Note: In the above syntax, the arr parameter indicates the input array which may be in the form of str or Unicode.

Returned Values:

This function will return the copy of the string having uppercased values in lowercase and vice-versa.

Let us cover some examples related to this function.

Example 1:

In the code snippet given below, we will apply swapcase() function on an array containing string:

import numpy as np

inp = np.array(['A4B C', '4c Rb', 'B Ec4', 'rp4q']) 
print("The input array is: ") 

outp = np.char.swapcase(inp) 
print("The output swapcased array :") 

The input array is:
['A4B C' '4c Rb' 'B Ec4' 'rp4q']
The output swapcased array :
['a4b c' '4C rB' 'b eC4' 'RP4Q']

Example 2:

Let's have another example:

import numpy as np

a= np.array(['WELCOMe', 'to', 'StuDyToNIGHT']) 

print ("The input array is: ", a) 

b = np.char.swapcase(a) 
print ("The output swapcased array :", b) 

The input array is: ['WELCOMe' 'to' 'StuDyToNIGHT']
The output swapcased array : ['welcomE' 'TO' 'sTUdYtOnight']


In this tutorial we learned about swapcase() function in the Numpy Library. We covered how it is used with its syntax and values returned by this function along with multiple code examples.