The lstrip() method will remove leading whitespaces, newline and tab characters on a string beginning:
>>> ' hello world!'.lstrip()
'hello world!'
Edit
As balpha pointed out in the comments, in order to remove only spaces from the beginning of the string, lstrip(' ') should be used:
>>> ' hello world with 2 spaces and a tab!'.lstrip(' ')
'\thello world with 2 spaces and a tab!'
Related question:
- Trimming a string in Python
Top answer 1 of 7
453
The lstrip() method will remove leading whitespaces, newline and tab characters on a string beginning:
>>> ' hello world!'.lstrip()
'hello world!'
Edit
As balpha pointed out in the comments, in order to remove only spaces from the beginning of the string, lstrip(' ') should be used:
>>> ' hello world with 2 spaces and a tab!'.lstrip(' ')
'\thello world with 2 spaces and a tab!'
Related question:
- Trimming a string in Python
2 of 7
128
The function strip will remove whitespace from the beginning and end of a string.
my_str = " text "
my_str = my_str.strip()
will set my_str to "text".
07:37
How To Remove Spaces From A String In Python
Remove Whitespace from Start and End (Regular Expressions ...
05:05
How to Remove Whitespace from String in Python (5 Examples) | ...
01:47
#28 Python Tutorial for Beginners | Python Remove WhiteSpace | ...
03:11
Regular Expressions - Remove Whitespace from Start and End - Free ...
DigitalOcean
digitalocean.com โบ community โบ tutorials โบ python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
July 11, 2025 - The strip() method is used to remove whitespace from the start and end of a string. For removing whitespace from the entire string, replace() can be used, and for more sophisticated patterns, you can use regular expressions via the re module. When using print() with multiple arguments, Python ...
GeeksforGeeks
geeksforgeeks.org โบ python โบ python-remove-spaces-from-a-string
Remove spaces from a string in Python - GeeksforGeeks
Explanation: s.strip() removes spaces from the start and end of s. If we only want to remove spaces from the beginning of the string, we can use lstrip().
Published ย July 11, 2025
Python Examples
pythonexamples.org โบ python-strip-remove-white-spaces-start-end-string
Trim white spaces from String in Python
In this tutorial, we will go through examples that demonstrate how to use String.strip() function to trim whitespaces from a given string. In the following example, we assign a variable with a string that has spaces at start and end of it. Then we use strip() function to remove the spaces around ...
Mimo
mimo.org โบ tutorials โบ python โบ how-to-remove-spaces-from-a-string-in-python
How to Remove Spaces from a String in Python
Use .strip() when the problem is extra whitespace at the start or end of a string.
Net Informations
net-informations.com โบ python โบ pro โบ space.htm
How to remove spaces from a string in Python
For the purpose of removing leading and trailing spaces within a string, the strip() method serves as the appropriate tool. This function effectively trims the whitespace from both the start and end of the string, resulting in a cleaner and more focused textual representation.
W3Schools
w3schools.com โบ PYTHON โบ ref_string_strip.asp
Python String strip() Method
Python Examples Python Compiler ... Plan Python Interview Q&A Python Bootcamp Python Training ... The strip() method removes any leading, and trailing whitespaces. Leading means at the beginning of the string, trailing means at ...
CodeFatherTech
codefather.tech โบ home โบ blog โบ how do you remove spaces from a python string?
How Do You Remove Spaces From a Python String? - CodeFatherTech
June 27, 2025 - There are multiple ways to remove spaces from a Python string. The simplest approach is to use the string replace() method. If the spaces to remove are just at the beginning and at the end of the string the strip() method also works fine. An alternative approach is to use a regular expression.
Python Morsels
pythonmorsels.com โบ remove-spaces
How to remove spaces in Python - Python Morsels
January 25, 2022 - Need to remove all spaces from a string in Python? If it's just space characters you could use the string replace method to replace all spaces by an empty string. If we call the replace method on this greeting string: >>> greeting = " Hello world!