Examples
- Palindrome Program in Python (Check String is Palindrome or Not)
- Program to Remove Punctuations From String in Python
- Remove a Character from String in Python (Program With Examples)
- Remove Stop Words from String in Python Using NLTK and spaCy
- Program to Sort Words in Alphabetical Order in Python (Arrange in Alphabetic Order)
- How to Sort Strings in Python? (Program With Examples)
- How to Count Vowels in Python String? Vowels Program in Python
- How to Remove Vowels from String in Python? Program With Examples
- How to Convert String to Int or Float in Python? String Parse Program
- How to Convert Float to Int in Python? Program With Examples
- How to Convert Int to String in Python? Program with Examples
- Remove Spaces from String in Python (Trim Whitespace)
- Python Program to Check If Two Strings are Anagram
- How to Capitalize First Letter in Python? String Capitalization Program
- Find All Permutations of String in Python (Programs and Examples)
- Find All Substrings of a String in Python (Programs with Examples)
- Create Multiline String in Python (With & Without New Line)
Remove Spaces from String in Python (Trim Whitespace)
In this tutorial, we will learn how to remove whitespace from strings in Python. Whether you're a beginner or an experienced programmer, mastering this essential skill will greatly enhance your ability to process and manipulate textual data efficiently.
Whitespace are the spaces, tabs, and newline characters that often exist as padding or formatting within strings. Removing such whitespace is crucial for various programming tasks, as it ensures:
-
data cleanliness,
-
improves readability,
-
enhances data processing accuracy.
Hence, let’s understand how to remove spaces from strings in Python, with practical programs with clear examples and explanations for better understanding.
To gain hands-on experience and sharpen your skills, you must use the online Python compiler by Tutorials Freak. This powerful tool offers a user-friendly interface, allowing you to write, execute, and experiment with Python Programs directly in your web browser.
Program to Remove Spaces from String in Python
Here's a simple Python program to remove white space from a given string:
Code
def remove_spaces(string):
return string.replace(" ", "")
# Example usage
input_string = "Tutorials Freak"
output_string = remove_spaces(input_string)
print(output_string)
Output
TutorialsFreak
Explanation
The `remove_spaces` function takes a string as input and uses the `replace()` method to replace all occurrences of a space character (" ") with an empty string (""). The modified string is then returned.
In the example, the input string is "Tutorials Freak". After removing the spaces, the resulting string is "TutorialsFreak".
Remove Space and Comma from String in Python
To remove both spaces and commas from a string in Python, you can modify the above program, as given below:
Code
def remove_spaces_and_commas(string):
string = string.replace(" ", "")
string = string.replace(",", "")
return string
# Example usage
input_string = "Thanks, Tutorias Freak!"
output_string = remove_spaces_and_commas(input_string)
print(output_string)
Output
ThanksTutoriasFreak!
Explanation
In the `remove_spaces_and_commas` function, we first use the `replace()` method to remove spaces from the string by replacing them with an empty string. Then, we use `replace()` again to remove commas from the string in the same way.
In the example, the input string is "Thanks, Tutorials Freak!". After removing spaces and commas, the resulting string is "ThanksTutorialsFreak!", which is printed to the console.