Examples :
Input: geek Output: geek Input: Hello World Output: HelloWorld
There are various approaches for removing whitespace from a string. The first — this is the Naive approach discussed in
# Python3 code to remove spaces def remove (string): return string.replace ( "" , "") # Driver program string = ’geek’ print (remove (string)) |
table> B Output:
geek
Approach # 2 Using split ()
and join ()
First we use the split ()
function to return a list of words in a string using sep as the separator string ... We then use join ()
to join the iterable.
# Python3 code to remove spaces def remove (string): return "". join (string.split ()) # Driver program string = ’geek’ print (remove (string)) |
Output:
geek
Approach # 3: Using Python Regular Expressions
# Python3 code to remove spaces
import
re
def
remove (string):
pattern
=
re.
compile
( r
’s +’
)
return
re.sub (pattern, ’’, string)
# Driver program
string
=
’geek’
print
(remove (string))
td>
Exit:
geek
Approach # 4: Using translate()
# Python code to remove spaces import string def remove (string): return string.translate ( None , ’’ ) # Driver program string = ’geek’ print (remove (string)) |
Exit:
geek
Shop
Learn programming in R: courses
$
Best Python online courses for 2022
$
Best laptop for Fortnite
$
Best laptop for Excel
$
Best laptop for Solidworks
$
Best laptop for Roblox
$
Best computer for crypto mining
$
Best laptop for Sims 4
$
Latest questions
NUMPYNUMPY
psycopg2: insert multiple rows with one query
12 answers
NUMPYNUMPY
How to convert Nonetype to int or string?
12 answers
NUMPYNUMPY
How to specify multiple return types using type-hints
12 answers
NUMPYNUMPY
Javascript Error: IPython is not defined in JupyterLab
12 answers
Wiki
Python OpenCV | cv2.putText () method
numpy.arctan2 () in Python
Python | os.path.realpath () method
Python OpenCV | cv2.circle () method
Python OpenCV cv2.cvtColor () method
Python - Move item to the end of the list
time.perf_counter () function in Python
Check if one list is a subset of another in Python
Python os.path.join () method