w3resource

NumPy String: numpy.char.swapcase() function

numpy.char.swapcase() function

The numpy.char.swapcase() function returns a new array with uppercase characters converted to lowercase and lowercase characters converted to uppercase.

This function is useful when working with text data where the case of the characters may be relevant for analysis or presentation purposes.

Syntax:

numpy.char.swapcase(a)

Parameter:

Name Description Required /
Optional
a Input array. [array_like, {str, unicode}]

Return value:

out [ndarray, {str, unicode}] Output array of str or unicode, depending on input type.

Example: Converting string case using numpy.char.swapcase()

>>> import numpy as np
>>> x=np.array(['a1B c','1b Ca','b Ca1','cA1b'],'S5'); x
array([b'a1B c', b'1b Ca', b'b Ca1', b'cA1b'], dtype='|S5')
>>> np.char.swapcase(x)
array([b'A1b C', b'1B cA', b'B cA1', b'Ca1B'], dtype='|S5')

In the above code, the np.char.swapcase() function is used to swap the case of each character in the string, i.e., uppercase letters are converted to lowercase, and vice versa. The result is a new numpy array with swapped case elements 'A1b C', '1B cA', 'B cA1', and 'Ca1B'.

Pictorial Presentation:

NumPy String operation: swapcase() function

Python - NumPy Code Editor:

Previous: strip()
Next: title()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/numpy/string-operations/swapcase.php