xxxxxxxxxx
for i in range(0,(len(list))):
x = str(list[i]).strip(' ')
list[i] = x
xxxxxxxxxx
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)}"')
xxxxxxxxxx
s = ' Hello World From Pankaj \t\n\r\t Hi There '
>>> s.replace(" ", "")
'HelloWorldFromPankaj\t\n\r\tHiThere'
xxxxxxxxxx
#If you want to remove LEADING and ENDING spaces, use str.strip():
sentence = ' hello apple'
sentence.strip()
>>> 'hello apple'
xxxxxxxxxx
>>> message = " Welcome to Codefather.tech "
>>> print(message.replace(" ",""))
WelcometoCodefather.tech
xxxxxxxxxx
final_res_list = ["a", "S", "Q", "3", "*", "5"]
print(*final_res_list, sep="")
#Output:
#aSQ3*5