NumPy String: numpy.char.title() function
numpy.char.title() function
The numpy.char.title() function returns element-wise title cased (First character of each word in uppercase and the rest in lowercase.) version of string or unicode.
This function can be used to standardize the capitalization of words in a given string or set of strings.
Syntax:
numpy.char.title(a)
Parameter:
Name | Description | Required / Optional |
---|---|---|
a: array_like, {str, unicode} | Input array. | Required |
Return value:
out [ndarray] Output array of str or unicode, depending on input type.
Example: Converting first character to uppercase using numpy.char.title()
>>> 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.title(x)
array([b'A1B C', b'1B Ca', b'B Ca1', b'Ca1B'], dtype='|S5')
In the above example, the numpy.char.title() function is applied on the array x which converts the first character of each word in the string to uppercase and returns a new array of the same shape and type as x.
Pictorial Presentation:
Example: Capitalizing the first character of each word in a string using numpy.char.title()
>>> import numpy as np
>>> x = np.array('the quick brown fox');
>>> x
array('the quick brown fox', dtype='<U19')
>>> np.char.title(x)
array('The Quick Brown Fox', dtype='<U19')
In the above code the numpy.char.title() function is applied to the array 'x', which returns a new array of strings with the first character of each word capitalized. Using the np.char.title() function, the string 'the quick brown fox' is converted to 'The Quick Brown Fox'.
Pictorial Presentation:
Python - NumPy Code Editor:
Previous:
swapcase()
Next:
translate()
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://www.w3resource.com/numpy/string-operations/title.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics