Created
May 29, 2020 15:11
-
-
Save pratik7368patil/9930464cf95684c4dd33dfd870c2a874 to your computer and use it in GitHub Desktop.
Remove Whitespaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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