Skip to content

Instantly share code, notes, and snippets.

@pratik7368patil
Created May 29, 2020 15:11
Show Gist options
  • Save pratik7368patil/9930464cf95684c4dd33dfd870c2a874 to your computer and use it in GitHub Desktop.
Save pratik7368patil/9930464cf95684c4dd33dfd870c2a874 to your computer and use it in GitHub Desktop.
Remove Whitespaces
# it's very easy to remove white spaces from strings
# it can be done using many methods but here we will disscuss some efficient one
# 1. Using replace()
s = " h a c k you r co de " # this function doesn't modify original string
new_s = s.replace(" ","") # here new_s holds new string with no blank spaces at all.
print(new_s)
# the output will be "hackyourcode"
# 2. Using join() and split()
s = " h a c k you r co de "
new_s = "".join(s.split()) # split() will return list of words
print(new_s) # then join() convert them into one string
# the output will be "hackyourcode"
# this problem can be solved using regex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment