Return an array with the elements of `a` right-justified in a string of length `width`. Parameters ---------- a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype width : array_like, with any integer dtype The length of the resulting strings, unless
(a, width, fillchar=' ')
| 825 | @set_module("numpy.strings") |
| 826 | @array_function_dispatch(_just_dispatcher) |
| 827 | def rjust(a, width, fillchar=' '): |
| 828 | """ |
| 829 | Return an array with the elements of `a` right-justified in a |
| 830 | string of length `width`. |
| 831 | |
| 832 | Parameters |
| 833 | ---------- |
| 834 | a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype |
| 835 | |
| 836 | width : array_like, with any integer dtype |
| 837 | The length of the resulting strings, unless ``width < str_len(a)``. |
| 838 | fillchar : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype |
| 839 | Optional padding character to use (default is space). |
| 840 | |
| 841 | Returns |
| 842 | ------- |
| 843 | out : ndarray |
| 844 | Output array of ``StringDType``, ``bytes_`` or ``str_`` dtype, |
| 845 | depending on input types |
| 846 | |
| 847 | See Also |
| 848 | -------- |
| 849 | str.rjust |
| 850 | |
| 851 | Notes |
| 852 | ----- |
| 853 | While it is possible for ``a`` and ``fillchar`` to have different dtypes, |
| 854 | passing a non-ASCII character in ``fillchar`` when ``a`` is of dtype "S" |
| 855 | is not allowed, and a ``ValueError`` is raised. |
| 856 | |
| 857 | Examples |
| 858 | -------- |
| 859 | >>> import numpy as np |
| 860 | >>> a = np.array(['aAaAaA', ' aA ', 'abBABba']) |
| 861 | >>> np.strings.rjust(a, width=3) |
| 862 | array(['aAaAaA', ' aA ', 'abBABba'], dtype='<U7') |
| 863 | >>> np.strings.rjust(a, width=9) |
| 864 | array([' aAaAaA', ' aA ', ' abBABba'], dtype='<U9') |
| 865 | |
| 866 | """ |
| 867 | width = np.asanyarray(width) |
| 868 | if not np.issubdtype(width.dtype, np.integer): |
| 869 | raise TypeError(f"unsupported type {width.dtype} for operand 'width'") |
| 870 | |
| 871 | a = np.asanyarray(a) |
| 872 | fillchar = np.asanyarray(fillchar) |
| 873 | |
| 874 | if np.any(str_len(fillchar) != 1): |
| 875 | raise TypeError( |
| 876 | "The fill character must be exactly one character long") |
| 877 | |
| 878 | if np.result_type(a, fillchar).char == "T": |
| 879 | return _rjust(a, width, fillchar) |
| 880 | |
| 881 | fillchar = fillchar.astype(a.dtype, copy=False) |
| 882 | width = np.maximum(str_len(a), width) |
| 883 | shape = np.broadcast_shapes(a.shape, width.shape, fillchar.shape) |
| 884 | out_dtype = f"{a.dtype.char}{width.max()}" |