Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & amp; expandtabs ())



String Methods Part- 2

Other methods are discussed in this article

1. strip () : — This method is used to remove all leading and trailing characters mentioned in its argument.

2. lstrip () : — This method is used to remove all leading characters mentioned in its argument.

3. rstrip () : — This method is used to remove all trailing characters mentioned in its argument.

# Python code to demonstrate how it works
# strip (), lstrip () and rstrip ()

str = "--- pythonengineering ---"

 
# using strip () to remove all & # 39; - & # 39;

print ( "String after stripping all` -` is: " , end = "")

print ( str . strip ( ` -` ))

 
# using lstrip () to remove all trailing & # 39; - & # 39;

print ( "String after stripping all leading` -` is: " , end = "")

print ( str . lstrip ( `-` ))

 
# using rstrip () to remove all leading & # 39; - & # 39;

print ( "String after stripping all trailing` -` is: " , end = " ")

print ( str . rstrip ( `-` ))

Output:

 String after stripping all `-` is: pythonengineering String after stripping all leading `-` is: pythonengineering --- String after stripping all trailing` -` is: --- pythonengineering 

4. min (“string”) : — This function returns the alphabet of minimum value from a string.

5. max (“string”) : — This function returns the alphabet of maximum value from a string.

# Python code to demonstrate how it works
# min () and max ()

str = "pythonengineering"

 
# using min () to print the smallest character
# prints & # 39; e & # 39;

print ( " The minimum value character is: " + min ( str ))

 
# using max () to print the largest character
# prints & # 39; s & # 39;

print ( " The maximum value character is: " + max ( str ))

Output:

 The minimum value character is: e The maximum value character is: s 

6. maketrans () : — It is used to match the contents of line 1 to line 2 with the appropriate indices for subsequent translation using translate ().

7. translate () : — Used to swap inline elements matched with maketrans ().

# Python code to demonstrate how it works
# maketrans () and translate ()

from string import maketrans # for layouttrans ()

 

str = "pythonengineering"

 

str1 = " gfo "

str2 = "abc"

 
# using maktrans () to map str2 elements to str1

mapped = maketrans (str1, str2)

 
# using translate () to translate using display

print " The string after translation using mapped elements is: "

print   str . translate (mapped) 

Exit:

 The string after translation using mapped elements is: aeeksbcraeeks 

In the above code, “g” is replaced with “a”, “f” is replaced with “b” and “o” is replaced with “c” in the string using the translation function.

8.replace () : — This function is used to replace a substring with a new substring in a string. This function has 3 arguments.  The string to replace, the newline to replace, and the maximum value indicating the limit for overwriting the action (default is unlimited).

# Python code to demonstrate how it works
# replace ()

 

str = "nerdsfornerds is for nerds"

 

str1 = "nerds"

str2 = "geeks"

 
# using replace () to replace str2 with str1 in str
 # changes only 2 cases

print ( "The string after replacing strings is:" , end = "" )

print ( str . replace (str1, str2, 2 )) 

Output:

 The string after replacing strings is: pythonengineering is for nerds 

This method is provided by Chinmoy Lenka
9. expandtabs () : — It is used to replace all tabs ("/ t") with spaces or just spaces, using a specified tab size, which is optional. 
Syntax: string.tabsize (tabsize)
Parameters: specifies the number of characters to replace for one tab character. By default, the function accepts the tab size as 8.
Returned value: a string in which all tabs are replaced by spaces.

# Python code for illustration expandtabs ()

string = `GEEKS FOR GEEKS`

 
# No parameters, default size is 8

print (string.expandtabs ())

 
# tab size taken as 2

print (string.expandtabs ( 2 ))

 
# pa Tab size taken as 5

print (string.expandtabs ( 5 ))

Output:

 GEEKS FOR GEEKS GEEKS FOR GEEKS GEEKS FOR GEEKS 

This article is updated Manjeet Singh If you love Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by mailing the article [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.