VOOZH about

URL: https://www.geeksforgeeks.org/python/numpy-string-operations-swapcase-function/

⇱ swapcase() function - NumPy - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

swapcase() function - NumPy

Last Updated : 27 Sep, 2025

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.


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.


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.


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

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


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