Last active
August 29, 2015 14:21
-
-
Save Yagisanatode/6e82a33f24a91bb7b430 to your computer and use it in GitHub Desktop.
Removing Unwanted Whitespace in a String
This file contains 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
###Removing Unwanted Whitespace In A String### | |
Comment_1 = " How are you? " | |
Comment_2 = " How are you? " | |
def remove_whitespace_either_side(comment): | |
comment = comment.strip() | |
print(comment) | |
def remove_any_duplicated_white_space(comment): | |
comment = " ".join(comment.split()) | |
print (comment) | |
print("The first comment has no extra spaces between words") | |
remove_whitespace_either_side(Comment_1) | |
remove_any_duplicated_white_space(Comment_1) | |
print("") | |
print("The second comment has extra spaces between words") | |
remove_whitespace_either_side(Comment_2) | |
remove_any_duplicated_white_space(Comment_2) | |
""" | |
RESULT | |
>>> | |
The first comment has no extra spaces between words | |
How are you? | |
How are you? | |
The second comment has extra spaces between words | |
How are you? | |
How are you? | |
>>> | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment