Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • How to use wildcards in SQL SQL
  • create read update delete using Tkinter class
  • How To Check For Unwanted Characters Using Loops With Python Python Data Cleansing
  • How to data cleanse a database table Python Data Cleansing
  • How To Run Python Validation From Javascript Javascript
  • How to remove unwanted characters Python Data Cleansing
  • How To Compare CSV Files for Differences CSV
  • What are anonymous functions in Julia? array

how to remove spaces from a string

Posted on January 28, 2021February 9, 2021 By admin No Comments on how to remove spaces from a string

Have you encountered a problem where you have some spaces in a string that you do not need?

Data analytics can throw up lots of challenges we hope to help you solve this problem easily.

This problem can be quite common and can cause problems as follows:

  • Comparing strings does not work as expected.
  • Automated processes may fail as the presence of a space may make it fall over.

So lets look at some common ways to find these problems and then fix them.

Removing spaces at the start or end of the file using strip()

A common way to achieve this , will remove white spaces at the start and send of your string, BUT not in the middle.

This would be very helpful where you want to format some strings before loading them into a database, as the white spaces can cause the load to fail.

spacedstring = " Please remove my spaces! "
print("Before",spacedstring)
print("After",spacedstring.strip())

Output:
Before:  Please remove my spaces! 
After: Please remove my spaces!

Removing all spaces using str.replace()

Quite simply this removes all the white spaces in the string, resulting in all the characters form one long string.

The first part of the function looks to identify empty spaces, then the second part looks to remove it.

spacedstring = " Please remove my spaces! "
print(spacedstring.replace(" ", ""))

Output = Pleaseremovemyspaces!

Remove spaces using a regular expression

A regular expression is widely used across a number of languages , as a result is a very powerful way to search for pattern matching.

It looks to create a combination of characters that the computer programme understands and applies to a string.

The re.compile() method saves the regular expression for re use, which is know to be more efficient.

With re.sub() , it takes the string spacedstring, looks for “pattern” in it and removes it and replaces it with “”, which basically is nothing.

import re
spacedstring = " Please remove my spaces! "
pattern = re.compile(r'\s+')
sentence = re.sub(pattern, '', spacedstring)
print(sentence)

Output = Pleaseremovemyspaces!

Using the join and split function together

Using the below code has two steps:

(A)The split function() first of all splits all the words in a string into a list. By doing this it removes any spaces in the original string.

(B) the .join then pulls them all back together.

(C) Finally as you have “” at the start, it tells the program not to include any spaces between the words.

spacedstring = " Please remove my spaces! "
print("".join(spacedstring.split()))

Output = Pleaseremovemyspaces!

So there you have a number of different ways to remove spaces inside a string.

One final things to note about regular expressions covered above:

  • They are generally quite short in nature, less code needs to be written to implement.
  • When they were written, the performance was front and centre, as a result, they are very quick.
  • They work over a number of different languages, so no changes need to made.

regular expressions, strings Tags:pattern matching, regex, remove spaces, remove spaces from string, split function

Post navigation

Previous Post: how do I merge two dictionaries in Python?
Next Post: how do I declare a null value in python?

Related Posts

  • How To Check For Unwanted Characters Using Loops With Python Python Data Cleansing
  • TypeError: expected str, bytes or os.PathLike object, not DataFrame Python
  • String Manipulation in Python python method
  • TypeError: type object is not subscriptable strings
  • ValueError: Columns must be same length as key exception handling
  • Regular expressions python Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • YouTube channel lists – Python Lists Python Lists
  • What is an Algorithm? machine learning
  • What is Assembly Language? Assembly Language
  • How to create a combobox in tkinter Python
  • YouTube channel lists – Tuples Python Tuples
  • hide a column from a data frame Python Dataframe
  • How to use parameters in Python Python
  • What is Query Optimization in SQL? SQL

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme