String Trimming in Python: All you Need to Know

Published on Oct 23,2019 1.6K Views

String Trimming in Python: All you Need to Know

edureka.co

Trimming a string can be very useful and it does have multiple applications. Many coders use the trimming function excessively to extract information, which might be valuable to them from an original string. In this article, we are going to discuss String Trimming in Python:

 

Introduction to Trimming

There are other functionalities that python possesses which might seem very similar to this. One such thing is “Slicing of Strings”. Here we can divide the strings into many parts and have different options applied on to the string. We can remove certain sections, cut out the first part, remove the last letters of the string and replace it with other strings in those positions as well.

Since that is a separate section altogether, let us now look at what can be done to strings via trimming.

 

What is String Trimming in Python?

The trimming of a string is done in three ways as depicted in the above diagram. Let us have a look at them.

By default, all these functions do not mandatorily require an argument to be passed to remove any whitespaces. Only when a certain character needs to be removed then that is mentioned in the argument and it will be accordingly removed from the leading and the trailing places.

str = ' EDUREKA '
print(f'String ='{s1}'n')
print(f'After Removing Leading Whitespaces String ='{str.lstrip()}'n')
print(f'After Removing Trailing Whitespaces String ='{str.rstrip()}'n')
print(f'After Trimming Whitespaces String ='{str.strip()}'n')

 

Now let us consider a certain character to be extracted from a string.

str = '&&&&&&EDUREKA&&&&&&'
print('n This is the orignial stringnn', str)
print('n Below is the usual strip function n')
print(str.strip('&'))
print('n Below is the R-strip functionnn')
print(str.rstrip('&'))
print('n Below is the L-strip functionnn')
print(str.lstrip('&'))

 

There are other functionalities, which revolve around this similar topic of string trimming in python. Let us now look at other simple functionalities, which are applied to strings.

 

Min and Max of a String

Here, the minimum function or the “min” is to extract the least value of the alphabet from the string. That is from the set of alphabets A-Z, A being the lowest value and Z being the highest. The “max” function does the opposite, which is, it picks out the highest value alphabet from the string. This can be better expressed with the example below.

str = 'EDUREKA'
print('n This is the orignial stringnn', str)
print('n The minimum value character is : n' + min(str))
print('nThe maximum value character is : n'+ max(str))

 

 

Replace

Replace function is quite simple to understand. From the word replace itself we can derive the meaning that certain sections of the string can be replaced with other string elements. For example, consider the below code:

str = 'EDUREKA is EDUREKA'
str1= 'EDUREKA'
str2= 'BEST'
print ("The final string after replacement is:n")
print("For one occurancen")
print (str.replace( str1, str2, 1))
print("nFor two occurancen")
print (str.replace( str1, str2, 2))

 

With this, we come to the end of this String Trimming in Python article. I hope you’ve got enough Ideas on String Trimming.

To get in-depth knowledge of Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of “String Trimming in Python” and we will get back to you.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 22nd June,2024

22nd June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Prompt Engineering Explained