Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
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')
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.
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:
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.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 25th January,2025 25th January SAT&SUN (Weekend Batch) | View Details |
edureka.co