- Популярные видео
- Авто
- Видео-блоги
- ДТП, аварии
- Для маленьких
- Еда, напитки
- Животные
- Закон и право
- Знаменитости
- Игры
- Искусство
- Комедии
- Красота, мода
- Кулинария, рецепты
- Люди
- Мото
- Музыка
- Мультфильмы
- Наука, технологии
- Новости
- Образование
- Политика
- Праздники
- Приколы
- Природа
- Происшествия
- Путешествия
- Развлечения
- Ржач
- Семья
- Сериалы
- Спорт
- Стиль жизни
- ТВ передачи
- Танцы
- Технологии
- Товары
- Ужасы
- Фильмы
- Шоу-бизнес
- Юмор
Remove Extra White Spaces from a String in Python
🧹 Remove Extra White Spaces from a String in Python | Using re.sub()
In this video, you'll learn how to remove extra white spaces from a sentence using Python's re module (re.sub() and re.compile). This is a powerful technique for cleaning up messy text, commonly used in data preprocessing and real-world automation tasks.
📌 What you'll learn:
✅ How to match one or more whitespace characters using \s+
✅ How to normalize spaces between words
✅ How to remove all spaces from a string
✅ How to strip leading and trailing spaces
🧠 Perfect for Python beginners, data cleaners, and interview practice.
🛠️ Code used in the video:
import re
# Input strings
str1 = " This is a sentence with white spaces. "
str2 = " P Y T H O N "
# Compile pattern for one or more spaces
space = re.compile(r'\s+')
# Normalize internal spaces and strip ends
result = space.sub(' ', str1).strip()
print(result) # Output: "This is a sentence with white spaces."
# Remove all spaces
result2 = space.sub('', str2).strip()
print(result2) # Output: "PYTHON"
💡 Tip: re.sub(r'\s+', ' ', text) replaces multiple spaces with a single space. Use strip() to remove leading and trailing spaces.
👍 Like, 💬 Comment, 🔁 Share & 🔔 Subscribe for more Python text cleaning and automation tutorials!
#Python #TextCleaning #RegexPython #RemoveWhiteSpaces #PythonForBeginners #reSub #LearnPython #PythonTips #StringManipulation #PythonCoding
Видео Remove Extra White Spaces from a String in Python канала Study Desk of Anand
In this video, you'll learn how to remove extra white spaces from a sentence using Python's re module (re.sub() and re.compile). This is a powerful technique for cleaning up messy text, commonly used in data preprocessing and real-world automation tasks.
📌 What you'll learn:
✅ How to match one or more whitespace characters using \s+
✅ How to normalize spaces between words
✅ How to remove all spaces from a string
✅ How to strip leading and trailing spaces
🧠 Perfect for Python beginners, data cleaners, and interview practice.
🛠️ Code used in the video:
import re
# Input strings
str1 = " This is a sentence with white spaces. "
str2 = " P Y T H O N "
# Compile pattern for one or more spaces
space = re.compile(r'\s+')
# Normalize internal spaces and strip ends
result = space.sub(' ', str1).strip()
print(result) # Output: "This is a sentence with white spaces."
# Remove all spaces
result2 = space.sub('', str2).strip()
print(result2) # Output: "PYTHON"
💡 Tip: re.sub(r'\s+', ' ', text) replaces multiple spaces with a single space. Use strip() to remove leading and trailing spaces.
👍 Like, 💬 Comment, 🔁 Share & 🔔 Subscribe for more Python text cleaning and automation tutorials!
#Python #TextCleaning #RegexPython #RemoveWhiteSpaces #PythonForBeginners #reSub #LearnPython #PythonTips #StringManipulation #PythonCoding
Видео Remove Extra White Spaces from a String in Python канала Study Desk of Anand
Комментарии отсутствуют
Информация о видео
22 августа 2025 г. 17:30:49
00:03:21
Другие видео канала




















