Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save giorgiberia/65fb18858fe23c0decb495e39ee42682 to your computer and use it in GitHub Desktop.
Save giorgiberia/65fb18858fe23c0decb495e39ee42682 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