Open In App

swapcase() function - NumPy

Last Updated : 27 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The numpy.char.swapcase() function is used to convert uppercase characters to lowercase and lowercase characters to uppercase in each string element of a NumPy array. It works element-wise and returns a new array with the modified strings.

For Example: This example demonstrates how np.char.swapcase() swaps the case of each character in a single string array.

Python
import numpy as np
arr = np.array(['Hello World'])
res = np.char.swapcase(arr)
print(res)

Output
['hELLO wORLD']

Syntax

numpy.char.swapcase(arr)

  • Parameters: arr array_like -> Input array of strings.
  • Return Value: ndarray -> New array with uppercase converted to lowercase and vice versa.

Examples

Example 1: In this example, we apply swapcase() on a string array containing alphanumeric values.

Python
import numpy as np
arr = np.array(['P4Q R', '4q Rp', 'Q Rp4', 'rp4q'])
res = np.char.swapcase(arr)
print("Array:", arr)
print("Result:", res)

Output
Array: ['P4Q R' '4q Rp' 'Q Rp4' 'rp4q']
Result: ['p4q r' '4Q rP' 'q rP4' 'RP4Q']

Example 2: In this example, we use swapcase() on an array of words.

Python
import numpy as np
arr = np.array(['Geeks', 'For', 'Geeks'])
res = np.char.swapcase(arr)
print("Array:", arr)
print("Result:", res)

Output
Array: ['Geeks' 'For' 'Geeks']
Result: ['gEEKS' 'fOR' 'gEEKS']

Example 3: Here, swapcase() is applied to sentences with mixed cases.

Python
import numpy as np
arr = np.array(['NumPy Is Powerful', 'PYTHON is Fun'])
res = np.char.swapcase(arr)
print("Array:", arr)
print("Result:", res)

Output
Array: ['NumPy Is Powerful' 'PYTHON is Fun']
Result: ['nUMpY iS pOWERFUL' 'python IS fUN']

Explore