- 📁 Applications
Blg Volta - 📁 Development
- 📁 Typescript
-
📁 Astro
Blg Directory Structure Of Blogs Collection Blg Astro Errors Blg Imagemetadata Blg Links Blg Search Tool -
📁 T4
Blg Custom Output Blg Editors Blg Template Content
Blg Chocolatey Blg Web Resources Blg Git Commands - 📁 Food
- 📁 Hardware
- 📁 Helpful
- 📁 Keyboard
-
📁 Ergogen
Blg Articles Blg Footprints Blg Get Keyboard Layout File From Ergogen Blg Ergogen Links Blg Mounting Hole In Outline
Blg Build Guides Blg Components Blg Kicad Examples Blg Keyboard Roadmap Blg Index Blg Keylayer Plate Thickness Blg Splitkb Blg Tools -
📁 Ergogen
| Blg | Roadmap |
Remove Whitespace
published: 2026-05-16
To remove whitespace from a Python string, select the method that matches your specific goal, as Python strings are immutable and return a new string.
Remove all whitespace (spaces, tabs, newlines): Use re.sub(r”\s+”, "", s) for robust pattern matching or "".join(s.split()) for high performance, as split() handles all unicode whitespace. Remove only literal space characters: Use s.replace(” ”, ""), which is simple but ignores tabs or newlines. Trim leading and trailing whitespace: Use s.strip(), or s.lstrip() and s.rstrip() for one-sided removal. Normalize multiple spaces between words: Use ” “.join(s.split()) to collapse all whitespace into single spaces while preserving word order. Fastest deletion of known characters: Use s.translate(str.maketrans(”, ”, string.whitespace)) for maximum speed when removing standard whitespace characters. import re import string
s = " Hello World\n\t "
# 1. Remove ALL whitespace (spaces, tabs, newlines)
clean_all_regex = re.sub(r"\s+", "", s)
clean_all_join = "".join(s.split())
# 2. Remove ONLY space characters (keeps \t and \n)
clean_spaces_only = s.replace(" ", "")
# 3. Trim ends only
trimmed = s.strip()
# 4. Normalize spaces (collapse multiple to single space)
normalized = " ".join(s.split())
# 5. Fastest removal of standard whitespace
clean_translate = s.translate(str.maketrans('', '', string.whitespace))