.replace(" ", "")
s = ' Hello World From Pankaj
Hi There '
>>> s.replace(" ", "")
'HelloWorldFromPankaj
HiThere'
>>> s.replace(" ", "")
words = " test words "
# Remove end spaces
def remove_end_spaces(string):
return "".join(string.rstrip())
# Remove first and end spaces
def remove_first_end_spaces(string):
return "".join(string.rstrip().lstrip())
# Remove all spaces
def remove_all_spaces(string):
return "".join(string.split())
# Remove all extra spaces
def remove_all_extra_spaces(string):
return " ".join(string.split())
# Show results
print(f'"{words}"')
print(f'"{remove_end_spaces(words)}"')
print(f'"{remove_first_end_spaces(words)}"')
print(f'"{remove_all_spaces(words)}"')
print(f'"{remove_all_extra_spaces(words)}"')
string=' t e s t '
print(string.replace(' ',''))
string = "Welcome to Python"
new_str = "".join(string.split(" "))
print(new_str) # "WelcometoPython"
sentence = ' hello apple '
sentence.strip()
>>> 'hello apple'
>>> " xyz ".rstrip()
' xyz'
>>> " hello apple ".replace(" ", "")
'helloapple'
sentence.replace(" ", "")
>>> " ".join(s.split())
'Hello World From Pankaj Hi There'
#If you want to remove LEADING and ENDING spaces, use str.strip():
sentence = ' hello apple'
sentence.strip()
>>> 'hello apple'