String strip methods

Dealing with the strings, many times programmers encounter the problem of undesirable character/characters at the end or beginning of the string, such as space or new line character at the end, for example,  " Baba saheb " and "Dr Ambedkar n".

To handle these problems, the Python string class comes loaded with three methods. We will discuss them one by one. Let's start with rstrip()

The syntax for the method is given as follows:

str1.rstrip([chars])

This method returns a copy of string str1 in which unwanted character/characters get removed from the right side of the string.

Consider the following example:

>>> str1 = "Dr Ambedkarn"
>>> str1.rstrip("n")
'Dr Ambedkar'

If you do not provide any chars as argument, then space is taken as default. Look at following the example:

>>> str2 =  " Baba Saheb "
>>> str2.rstrip()
' Baba Saheb'
>>>

If you want to strip from the left-side use the lstrip() method. If you want to strip from both sides, use the strip() method.

Consider the following example:

>>> str2
' Baba Saheb '
>>> str2.strip()
'Baba Saheb'
>>>
..................Content has been hidden....................

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