String justify methods

methods to deal with these types of situations.

In many situations, you might need to justify the string length. The requirement may be string, which must possess a certain length. We have four string methods to deal with these types of situations. Let's start from ljust(), which means left justify.

The syntax for the method is given as follows:

str1.ljust(width[, fillchar])

When you provide the string to the ljust() method, it returns the string left justified. The minimum string length is specified by width and the padding on the left side is specified by the fillchar character(s), the space is default.

Consider the following examples:

>>> str1= "Mohit Raj"
>>> str1.ljust(15, "#")
'Mohit Raj######'
>>>
>>> str2= "Bhaskar Narayan Das"
>>> str2.ljust(15, "#")
'Bhaskar Narayan Das'
>>>

In the preceding example, the length of str2 is greater than 15. So padding is not made, which means fillchars has not been used. Let's see the example of rjust, which does the same thing but justifies the length from the right side:

>>> str1= "Mohit Raj"
>>> str1.rjust(15, "#")
'######Mohit Raj'
>>>

Consider the situation where you want to justify the string from both sides. In this case, we will use the center() method. 

Let's see an example of center():

>>> str1= "Mohit Raj"
>>> str1.center(16, "#")
'###Mohit Raj####'
>>>

Sometimes when you are dealing with strings such as bank account number, binary numbers, and so on, you may need to justify the string with zeros. Although we can do this using the ljust method, Python offers you a special method called zfill().

The syntax for the method is given as follows:

str.zfill(width) 

This method pads the string on the left with zeros to fill the width.

Consider the following examples:

>>> acc_no =  "3214567987"
>>> acc_no.zfill(15)
'000003214567987'
>>>
>>> binary_num = "10101010"
>>> binary_num.zfill(16)
'0000000010101010'
>>>

Many times we deal with the situation where we may want to replace a word from a line or a substring from the string. Python's replace() string method can do the task.

replace()

The syntax for the method is as follows:

str.replace(old, new max)

This method returns a copy of the string in which the old character(s) are replaced with new character(s). The max parameter specifies the number of replacements. If no number is specified, then it means all the occurrences would be replaced.

Consider the following example:

>>> str1 = "time is great and time is money"
>>> str1.replace("is","was")
'time was great and time was money'
>>>
>>> str1
'time is great and time is money'
>>> str1.replace("is",1)
>>> str1.replace("is","was",1)
'time was great and time is money'
>>>

Consider you have a sequence (list or tuple) of string and you want to join values of the sequence.

Consider the sequence ["Mohit","raj"] and you want to make it "Mohit raj" or "Mohit-raj".

To deal with these type of situations, use the join method:

join()

The syntax for the method is:

str1.join(seq)

The seq  contains the sequence of separated strings; here,  str1 acts as a separator. Let’s see different examples of join():

  1. The space as the separator:
>>> name = ["Mohit","raj"]
>>> " ".join(name)
'Mohit raj'
>>>
  1. Nothing as separator:
>>> "".join(name)
'Mohitraj'
>>>
  1. A hyphen - as the separator:
>>> "-".join(name)
'Mohit-raj'
>>>

You can try with different-2 separators.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.178.53