Python

Python Trim String Example

1. Introduction

White spaces are good to have, but sometimes they appear where they are not needed. White spaces are basically what allows us to have one word from another. But no matter how good they sound, sometimes we do not want them appearing at the end of words that have no words after them. So during those times, we want to remove the ending or beginning white spaces. White spaces are produced when we hit the Return key, the Spacebar key, and the Tab key.

In python, we use the strip() method to remove white spaces at the beginning and at the end of strings. Once you have a string with white spaces preceding or ending it, we use the strip() method to remove such whitespaces.

1.0 Other Types Of The Strip() Method

Now, we consider the subtypes of the methodstrip() that we can have. We can have the rstrip() method which only removes the white spaces on the right and we have thelstrip() method which basically removes whitespaces on the left.

1.1 Syntax

The syntax of the strip() method is very simple. Simply call the strip() method on the string and it produces a string without the unnecessary white spaces. The syntax for the strip() method is as follows: string.strip()

The lstrip() and the rstrip() method all have the same syntax.

2. Python Trim String Examples

Let us consider some examples of how to use the strip()method.

2.1 Python Trim String Example 1

Consider we have the following string entered as the name of a user

username = '   Ismaila Isiakyu   '

So the above string contains whitespaces before and after the main string needed. So to remove the whitespaces, we use

 username.strip() 

And the output would be: 'Musa Isiakyu' removing all the unnecessary white spaces.

2.2 Python Trim String Example 2

Let us consider the next example.

food = '   Bitter Leaf   '

So to remove the unneeded whitespaces from the above, we simply call in the strip() method as follows:

food.strip()

Calling this would return a string that contains only ‘Bitter Leaf’ without the unnecessary whitespaces.

2.3 Python Trim String Example 3

Let us look at the lstrip() method. Imagine the string:

student = '    Water Board'

Based on what we can see, the left side of the code has the whitespaces. To remove white spaces on the left, we call the lstrip() method.

student.lstrip()

And this would return 'Water Board'

2.4 Python Trim String Example 4

Let us look at the rstrip() method. Imagine the string:

student = 'Water Board    '

Based on what we can see, the right side of the code has the whitespaces. To remove white spaces on the left, we call the methodrstrip().

student.rstrip()

And this would return 'Water Board'

2.5 Python Trim String Example 5

In this example, we understand the difference between the lstrip(), rstrip() and strip().

Consider the string:

studentName = '         New Name'

If we call the following:

studentName.lstrip()

The result returned would be: 'New Name'

And when we call

studentName.rstrip()

The result returned would be: '         New Name'

However, when we call:

studentName.strip()

We have 'New Name'

From the above example, we now understand better how the strip(), lstrip() and rstrip() works.

2.6 Python Trim String Example 6

And for the final example in this session, let us consider the string

finalString = '   New Student Final String       '

When we call

finalString.lstrip()

The output is 'New Student Final String        '

And the white spaces on the right would still be there. And when we call the following:

finalString.rstrip()

The output is ' New Student Final String'

And the left white spaces would still be there. But when we call

finalString.strip()

The result would be 'New Student Final String' removing the white spaces on both ends.

3. Conclusion

We learned the need for white spaces but also on how Python can help remove white spaces where they are not necessary. The strip(), lstrip() and rstrip() are some of the methods discussed.

4. Download The Source Code

This was an example of how to trim strings in Python using the strip(), lstrip() and rstrip() methods

Download
You can download the full source code of this example here: Python Trim String Example

Idris Azeez

Idris Azeez, is currently running a Masters program at the Nile University Of Nigeria, Abuja. He is a Java Developer with more than of 3 years programming experience. He has a strong passion for solving challenging tasks in the areas of health and education.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button