The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".
The trim() function removes whitespace and other predefined characters from both sides of a string.
strip(): The strip() method is the most commonly accepted method to remove whitespaces in Python. It is a Python built-in function that trims a string by removing all leading and trailing whitespaces.
The trim() method will remove both leading and trailing whitespace from a string and return the result.
I want to remove all spaces from a string.
" as fa sdf sdfsdf "
The result would be:
"asfasdfsdfsdf"
There are several ways I can think of to achieve this, and I'm wondering which one is the best.
1.
"".join(" as fa sdf sdfsdf ".split())
2.
" as fa sdf sdfsdf ".replace(" ", "")
And I assume that there are more.
Which one is preferred?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With