operations with empty strings | ljust () function

ljust | NumPy | Python Methods and Functions | String Variables

numpy.core.defchararray.ljust (arr, width, fillchar = & # 39; & # 39;) — this is another function for doing string operations in numpy. Returns an array with left-aligned arr elements in a length-width string. It fills the remaining space of each element in the array with the fillchr parameter fillchr . If fillchr is not passed, the remaining spaces are filled with blank space.

Parameters:
arr: array_like of str or unicode.Input array.
width: The final width of the each string.
fillchar: The character to fill in remaining space.

Returns: Output array of str or unicode, depending on input type.

Code # 1:

# Python program explaining
# numpy.char.ljust () method

 
# numpy import

import numpy as geek

 
# input array 

in_arr = geek.array ([ `Numpy` , ` Python` , `Pandas` ])

print ( "Input array:" , in_arr) 

 
# set the width of each line to 8

width = 8

 
# output array when fillchar is not supplied

out_arr = geek.char.ljust (in_arr, width)

print ( " Output left justified array: " , out_arr) 

Output :

 Input array: [`Numpy`` Python` `Pandas`] Output left justified array: [`Numpy` `Python` `Pandas`] 

Code # 2:

# Python program explaining
# numpy.char.ljust () method

 
# numpy import

import numpy as geek

 
# input array

in_arr = geek.array ([ `Numpy` `Python` , ` Pandas` ])

print ( " Input array: " , in_arr) 

  
# set the width of each line to 8

width = 8

 
# output array

out_arr = geek.char.ljust (in_arr, width, fillchar = `*` )

print ( "Output left justified array:" , out_ar r) 

Exit:

 Input array: [`Numpy`` Python` `Pandas`] Output left justified array: [` Numpy *** `` Python ** `` Pandas ** `] 

Code # 3:

# Python program explaining
# numpy. char.ljust () method

 
# numpy import

import numpy as geek

 
# input array

in_arr = geek.array ([ `1` , `11` , ` 111`  ])

print ( "Input array:" , in_arr)

 
# set the width of each line to 5

width = 5

 
#output array

out_arr = geek. char.ljust (in_arr, width, fillchar = `-` )

print ( "Output left justified array : " , out_arr) 

Output:

 Input array: [`1`` 11` `111`] Ou tput left justified array: [`1 ----` `11 ---` `111 -`] 




operations with empty strings | ljust () function: StackOverflow Questions

Answer #1

You can do this with str.ljust(width[, fillchar]):

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s).

>>> "hi".ljust(10)
"hi        "

Answer #2

For many objects, you can use this code, replacing "object" with the object you"re interested in:

object_methods = [method_name for method_name in dir(object)
                  if callable(getattr(object, method_name))]

I discovered it at diveintopython.net (now archived). Hopefully, that should provide some further detail!

If you get an AttributeError, you can use this instead:

getattr( is intolerant of pandas style python3.6 abstract virtual sub-classes. This code does the same as above and ignores exceptions.

import pandas as pd
df = pd.DataFrame([[10, 20, 30], [100, 200, 300]],
                  columns=["foo", "bar", "baz"])
def get_methods(object, spacing=20):
  methodList = []
  for method_name in dir(object):
    try:
        if callable(getattr(object, method_name)):
            methodList.append(str(method_name))
    except:
        methodList.append(str(method_name))
  processFunc = (lambda s: " ".join(s.split())) or (lambda s: s)
  for method in methodList:
    try:
        print(str(method.ljust(spacing)) + " " +
              processFunc(str(getattr(object, method).__doc__)[0:90]))
    except:
        print(method.ljust(spacing) + " " + " getattr() failed")

get_methods(df["foo"])

Answer #3

a += [""] * (N - len(a))

or if you don"t want to change a in place

new_a = a + [""] * (N - len(a))

you can always create a subclass of list and call the method whatever you please

class MyList(list):
    def ljust(self, n, fillvalue=""):
        return self + [fillvalue] * (n - len(self))

a = MyList(["1"])
b = a.ljust(5, "")

Get Solution for free from DataCamp guru