How can I test if a string contains only whitespace?
Example strings:
-
" "(space, space, space) -
" \t \n "(space, tab, space, newline, space) -
"\n\n\n\t\n"(newline, newline, newline, tab, newline)
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.
Use the
str.isspace()method:Combine that with a special case for handling the empty string.
Alternatively, you could use
str.strip()and check if the result is empty.