Python Programming (131 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (68 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)

String Trimming in Python: All you Need to Know

Published on Oct 23,2019 300 Views

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.

  • Strip – A new string is returned after removing any trailing spaces or leading spaces.

  • R-strip – This outputs a new string with only the trailing spaces removed. Hence the name “rstring” i.e removing the spaces only from the right side of the string.

  • L-Strip – “Lstrip” does the opposite of the R-strip. It removes the whitespaces from the beginning of the string, which is the left side.

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')

String Trimming in Python


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('n Below is the R-strip functionnn')
print('n Below is the L-strip functionnn')



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 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:

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.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.