Leading or trailing whitespace characters present in strings may not necessarily be useful. In this tutorial, we will look at how to trim whitespace characters from strings in python.
How to trim whitespaces in python strings?
Python strings come with built-in functions like lstrip()
, rstrip()
, and strip()
to trim whitespaces based on the use case. For example, you can use –
- The
lstrip()
function to trim leading whitespace characters. - The
rstrip()
function to trim trailing whitespace characters. - The
strip()
function to trim leading and the trailing whitespace characters.
Let’s look at the usage of these functions with the help of examples.
1. Remove leading whitespaces with lstrip()
The lstrip()
function is used to remove leading characters from a string and by default removes leading whitespace characters. It returns a copy of the string with the leading characters removed.
# string with leading whitespaces s = " \n\t John Doe" # show original string print(s) # remove leading whitespaces print(s.lstrip())
Output:
John Doe John Doe
You can see that the leading white spaces have been removed in the returned string. Note that, \n
and \t
are also considered whitespace characters in python.
Note that, you can use the lstrip()
function to remove any leading characters and not just whitespace characters. For example, let’s remove all leading vowels from a string.
# string s = "eat this burger" # show original string print(s) # remove leading vowels print(s.lstrip("aeiou"))
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
eat this burger t this burger
All the leading vowels (“ea” in this case) have been removed in the returned string.
2. Remove trailing whitespaces with rstrip()
The rstrip()
function is used to remove trailing characters from a string and by default removes trailing whitespace characters. It returns a copy of the string with the trailing characters removed.
# string with trailing whitespaces s = "John Doe\n\t " # show original string print(s) # remove trailing whitespaces print(s.rstrip())
Output:
John Doe John Doe
You can see that the trailing white spaces have been removed in the returned string.
Note that, you can use the rstrip()
function to remove any trailing characters and not just whitespace characters. For example, let’s remove all trailing vowels from a string.
# string s = "eat this burger from Rome" # show original string print(s) # remove trailing vowels print(s.rstrip("aeiou"))
Output:
eat this burger from Rome eat this burger from Rom
All the trailing vowels (“e” in this case) have been removed in the returned string.
3. Remove leading and trailing whitespaces with strip()
The strip()
function is used to remove both leading and trailing characters from a string and by default removes whitespace characters. It returns a copy of the string with the leading and the trailing characters removed.
# string with trailing whitespaces s = " \t\nJohn Doe\n\t " # show original string print(s) # remove leading and trailing whitespaces print(s.strip())
Output:
John Doe John Doe
You can see that the all the leading and the trailing white spaces have been removed in the returned string.
Note that, you can use the strip()
function to remove any trailing characters and not just whitespace characters. For example, let’s remove all leading and trailing vowels from a string.
# string s = "eat this burger from Rome" # show original string print(s) # remove leanding and trailing vowels print(s.strip("aeiou"))
Output:
eat this burger from Rome t this burger from Rom
All the leading and trailing vowels have been removed in the returned string.
For more on the strip() function, refer to its documentation.
You might also be interested in – Python – Remove Character From String
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples