Python String Methods ( Part 3 ) - GeeksforGeeks

12 May, 2022
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
# 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 the string.5. max(“string”) :- This function returns the maximum value alphabet from string. 
Python
# 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
# Python code to demonstrate working of # maketrans() and translate()from string import maketrans # for maketrans()  str = "geeksforgeeks"  str1 = "gfo"str2 = "abc"  # using maketrans() to map elements of str2 with str1mapped = maketrans( str1, str2 )  # using translate() to translate using the mappingprint "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 the 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
# 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 method is contributed by Chinmoy Lenka 9. expandtabs():- It is used to replace all tab characters(“\t”) with whitespace or simply spaces using the given tab size, which is optional to supply. Syntax: string.tabsize(tabsize) Parameters: Specifying the number of characters to be replaced for one tab character. By default, the function takes tab size as 8. Return Value: A string in which all the tab characters are replaced with spaces. 
Python3
# Python code to illustrate expandtabs()string = 'GEEKS\tFOR\tGEEKS'  # No parameters, by default size is 8print (string.expandtabs())  # tab size taken as 2print(string.expandtabs(2))  # tab size taken as 5print(string.expandtabs(5))
Output:  
GEEKS   FOR     GEEKS
GEEKS FOR GEEKS
GEEKS     FOR  GEEKS
 
YouTubeGeeksforGeeks507K subscribersPython Programming Tutorial | Python String Methods - Part 3 | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:42•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=ECnJ9AD6gHY" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
NelsonSelvam
reenadevi98412200
surinderdawra388
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python