About 9,680,000 results
Open links in new tab
  1. Remove spaces from a string in Python - GeeksforGeeks

    Apr 17, 2025 · To remove all spaces from a string, we can use replace () method. Explanation: s.replace (” “, “”) replaces every space in s with an empty string “”, effectively removing them. Removing Leading and Trailing Spaces. Sometimes, we only need to remove spaces from the start and end of a string while leaving the inner spaces untouched.

  2. python - Remove all whitespace in a string - Stack Overflow

    Oct 1, 2016 · To remove only spaces use str.replace: sentence = sentence.replace(' ', '') To remove all whitespace characters (space, tab, newline, and so on) you can use split then join: sentence = ''.join(sentence.split()) or a regular expression: import re pattern = re.compile(r'\s+') sentence = re.sub(pattern, '', sentence)

  3. python - Remove all special characters, punctuation and spaces

    Jun 11, 2015 · I need to remove all special characters, punctuation and spaces from a string so that I only have letters and numbers. This can be done without regex: You can use str.isalnum: and there is at least one character in S, False otherwise. If you insist on using regex, other solutions will do fine.

  4. Python : How to compare strings and ignore white space and …

    Generally, you'd replace the characters you wish to ignore, and then compare them: import re def equal(a, b): # Ignore non-space and non-word characters regex = re.compile(r'[^\s\w]') return regex.sub('', a) == regex.sub('', b) >>> equal('Hai, this is a test', 'Hai this is a test') True >>> equal('Hai, this is a test', 'Hai this@#)($! i@#($()@# ...

  5. Effective Ways to Remove Spaces from Strings in Python

    Dec 18, 2024 · Master Python string manipulation by learning how to remove whitespace and manage spaces with techniques like strip(), replace(), and regex for cleaner data.

  6. Python – Avoid Spaces in string length - GeeksforGeeks

    Jan 14, 2025 · Let’s explore different methods to find the length of a string while ignoring spaces. Using replace() replace() function allows us to remove all spaces from the string before calculating the length.

  7. Python | Remove unwanted spaces from string - GeeksforGeeks

    Apr 23, 2023 · In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence.

  8. How to Remove Spaces from String in Python? - Python Guides

    Jan 31, 2025 · In this tutorial, we explored several methods to remove spaces from string in Python, including str.replace() method, str.split() and str.join() method, regular expressions, str.strip(), str.lstrip() and st.rstrip() methods, and str.translate() method with examples.

  9. Python Remove Spaces from String - AskPython

    Dec 29, 2019 · Python provides various ways to remove white-spaces from a String. This article will focus of some of the efficient techniques to remove spaces from a String. Either of the following techniques can be used to get rid of the spaces from a string: By using strip() method; By using replace() method; By using join() with split() method; By using ...

  10. 5 Best Ways to Handle String Whitespace in Python

    Mar 11, 2024 · This article explores the best methods to deal with whitespace in strings using Python. Method 1: Using String strip() Method The strip() method in Python is designed to return a new string after removing any leading and trailing whitespaces (including spaces, tabs, and …

  11. Some results have been removed
Refresh