Python Check String for Spaces Only

You can use the isspace() method to check if the string contains only space characters.

Here is an example code:

string = "   "
if string.isspace():
    print("字符串中只包含空格字符")
else:
    print("字符串中不只包含空格字符")

If the string only contains whitespace characters, output “The string only contains whitespace characters”; otherwise, output “The string does not only contain whitespace characters.”

bannerAds