What is the numpy.char.swapcase() function in Python?

Overview

The char.swapcase() function in NumPy returns a copy of a string or array, depending on the input whose uppercase characters are converted to lowercase and vice versa, element-wise. In simpler words, all the uppercase elements are converted to lowercase, while all the lowercase elements are converted to uppercase.

Syntax

char.swapcase(a)

Parameter value

The char.swapcase() function takes a single and mandatory parameter value, a, which represents the input array or the string.

Return value

The char.swapcase() function returns an output array of strings or Unicodes, depending on the input type that is passed to the function.

Code example

import numpy as np
# creating an array
myarray = np.array(["THAT", "There", "ThESE", "they"])
# implementing the char.swapcase() function
print(np.char.swapcase(myarray))

Code explanation

We converted all the uppercase characters of each element of the input array called myarray to lowercase characters and vice versa, using the char.swapcase() function.

Free Resources