Skip to content

Instantly share code, notes, and snippets.

@Yagisanatode
Last active August 29, 2015 14:21
Show Gist options
  • Save Yagisanatode/6e82a33f24a91bb7b430 to your computer and use it in GitHub Desktop.
Save Yagisanatode/6e82a33f24a91bb7b430 to your computer and use it in GitHub Desktop.
Removing Unwanted Whitespace in a String
###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