Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate & relplace)

Some of the string methods are covered in below sets.

String Methods Part- 1
String Methods Part- 2

More methods are discussed in this article

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

2. lstrip() :- This method is used to delete all the leading characters mentioned in its argument.

3. rstrip() :- This method is used to delete all the trailing characters mentioned in its argument.

# Python code to demonstrate working of 
# strip(), lstrip() and rstrip()
str = "---geeksforgeeks---"

# using strip() to delete all '-'
print ( " String after stripping all '-' is : ", end="")
print ( str.strip('-') )

# using lstrip() to delete all trailing '-'
print ( " String after stripping all leading '-' is : ", end="")
print ( str.lstrip('-') )

# using rstrip() to delete all leading '-'
print ( " String after stripping all trailing '-' is : ", end="")
print ( str.rstrip('-') )

Output:

 String after stripping all '-' is : geeksforgeeks
 String after stripping all leading '-' is : geeksforgeeks---
 String after stripping all trailing '-' is : ---geeksforgeeks

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

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

# Python code to demonstrate working of 
# min() and max()
str = "geeksforgeeks"

# using min() to print the smallest character
# prints 'e'
print ("The minimum value character is : " + min(str));

# using max() to print the largest character
# prints 's'
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 map the contents of string 1 with string 2 with respective indices to be translated later using translate().

7. translate() :- This is used to swap the string elements mapped with the help of maketrans().

# Python code to demonstrate working of 
# maketrans() and translate()
from string import maketrans # for maketrans()

str = "geeksforgeeks"

str1 = "gfo"
str2 = "abc"

# using maktrans() to map elements of str2 with str1
mapped = maketrans( str1, str2 );

# using translate() to translate using the mapping
print "The string after translation using mapped elements is : "
print  str.translate(mapped) ;

Output:

The string after translation using mapped elements is : 
aeeksbcraeeks

In the above code, ‘g’ is replaced by a, ‘f’ is replaced by b and ‘o’ is replaced by ‘c’ in the string using translate function.

8. replace() :- This function is used to replace the substring with a new substring in the string. This function has 3 arguments. The string to replace, new string which would replace and max value denoting the limit to replace action ( by default unlimited ).

# Python code to demonstrate working of 
# replace()

str = "nerdsfornerds is for nerds"

str1 = "nerds"
str2 = "geeks"

# using replace() to replace str2 with str1 in str
# only changes 2 occurrences 
print ("The string after replacing strings is : ", end="")
print (str.replace( str1, str2, 2)) 

Output:

The string after replacing strings is : geeksforgeeks is for nerds

This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.