How to Remove Spaces from String in Python | Python Regex

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see how to remove spaces from String in Python.
We have different solutions to fulfil this requirement in python. Let’s see one by one.
Python rstrip() function is used to remove the spaces at the right side of a string.
str = ' hello  python string    '
str = str.rstrip()
print(str)

Output:
hello  python string
Python lstrip() function is used to remove the spaces at the left side of a string.
str = '      hello  python string '
str = str.lstrip()
print(str)
Output:
hello python string
Python strip() function is used to remove the spaces at both sides of a string.
str = '      hello python string    '
str = str.strip()
print(str)
Output:
hello python string
replace() function used to replace all the matching contents with the given content. Here I am going to replace all the white spaces with empty so that I can remove all white areas from the string.
str = '  hello python string    '
str= sentence.replace(" ", "")
print(str)
Output:
hellopythonstring
join() function is used to join the array of strings.
str = ' hello  python string    '
str = " ".join(str.split())
print(str)
Output:
hello python string
import re

# Reg Expression
# Removing Begining of a String
str = '    hello  python   '
str = re.sub(r"^\s+", "", str, flags=re.UNICODE)
print("removed spaces at left side :",str)

# Ending of a string
str = '    hello  python   '
str = re.sub(r"\s+$", "", str, flags=re.UNICODE)
print("removed spaces at right side :",str)

# Removing Begining and Ending
str = '    hello  python   '
str = re.sub("^\s+|\s+$", "", str, flags=re.UNICODE)
print("removed spaces at both sides :",str)

# Removing all spaces
str = ' hello  python   '
pattern = re.compile(r'\s+')
str = re.sub(pattern, '', str)
print(str)
Output:
removed spaces at left side : hello  python   
removed spaces at right side :     hello  python
removed spaces at both sides : hello  python
removed all spaces: hellopython
Done!
Python Regular Exp guide
Python How to read string input from the keyboard
Happy Learning 🙂

Python How to read input from keyboard
Python Set Data Structure in Depth
Python raw_input read input from keyboard
Python TypeCasting for Different Types
Python List Data Structure In Depth
JQuery Remove Elements Example
Python – How to remove key from dictionary ?
Python – How to remove duplicate elements from List
Python – Print different vowels present in a String
Python String to int Conversion Example
PHP Comments , White Spaces and Case Sensitivity
How to merge two lists in Python
How to Read CSV File in Python
How to get the size of a Directory in Python ?
Python – Find the biggest of 2 given numbers

Python How to read input from keyboard
Python Set Data Structure in Depth
Python raw_input read input from keyboard
Python TypeCasting for Different Types
Python List Data Structure In Depth
JQuery Remove Elements Example
Python – How to remove key from dictionary ?
Python – How to remove duplicate elements from List
Python – Print different vowels present in a String
Python String to int Conversion Example
PHP Comments , White Spaces and Case Sensitivity
How to merge two lists in Python
How to Read CSV File in Python
How to get the size of a Directory in Python ?
Python – Find the biggest of 2 given numbers
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list