MCPcopy
hub / github.com/numpy/numpy / ljust

Function ljust

numpy/_core/strings.py:762–822  ·  view source on GitHub ↗

Return an array with the elements of `a` left-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=' ')

Source from the content-addressed store, hash-verified

760@set_module("numpy.strings")
761@array_function_dispatch(_just_dispatcher)
762def ljust(a, width, fillchar=' '):
763 """
764 Return an array with the elements of `a` left-justified in a
765 string of length `width`.
766
767 Parameters
768 ----------
769 a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype
770
771 width : array_like, with any integer dtype
772 The length of the resulting strings, unless ``width < str_len(a)``.
773 fillchar : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype
774 Optional character to use for padding (default is space).
775
776 Returns
777 -------
778 out : ndarray
779 Output array of ``StringDType``, ``bytes_`` or ``str_`` dtype,
780 depending on input types
781
782 See Also
783 --------
784 str.ljust
785
786 Notes
787 -----
788 While it is possible for ``a`` and ``fillchar`` to have different dtypes,
789 passing a non-ASCII character in ``fillchar`` when ``a`` is of dtype "S"
790 is not allowed, and a ``ValueError`` is raised.
791
792 Examples
793 --------
794 >>> import numpy as np
795 >>> c = np.array(['aAaAaA', ' aA ', 'abBABba'])
796 >>> np.strings.ljust(c, width=3)
797 array(['aAaAaA', ' aA ', 'abBABba'], dtype='<U7')
798 >>> np.strings.ljust(c, width=9)
799 array(['aAaAaA ', ' aA ', 'abBABba '], dtype='<U9')
800
801 """
802 width = np.asanyarray(width)
803 if not np.issubdtype(width.dtype, np.integer):
804 raise TypeError(f"unsupported type {width.dtype} for operand 'width'")
805
806 a = np.asanyarray(a)
807 fillchar = np.asanyarray(fillchar)
808
809 if np.any(str_len(fillchar) != 1):
810 raise TypeError(
811 "The fill character must be exactly one character long")
812
813 if np.result_type(a, fillchar).char == "T":
814 return _ljust(a, width, fillchar)
815
816 fillchar = fillchar.astype(a.dtype, copy=False)
817 width = np.maximum(str_len(a), width)
818 shape = np.broadcast_shapes(a.shape, width.shape, fillchar.shape)
819 out_dtype = f"{a.dtype.char}{width.max()}"

Callers 1

ljustMethod · 0.85

Calls 3

astypeMethod · 0.80
anyMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…