How would I collapse run-on whitespace in python?
"a b c d e" --> "a b c d e"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming
then
will give you the specified output.
This works by using split() to break the string into into a list of individual characters
['a', 'b', 'c', 'd', 'e']and then joining them again with a single space in-between using the join() function into a string. Thesplit()also takes care of any leading or trailing blanks.Based on Simple is better than complex (Zen of Python) in order to avoid the regex “two problem” problem 🙂