Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเช็คว่า Text เป็น Empty (spaces, tabs, newlines) ใน Python

1. ใช้ isspace()

>> print('Hello world'.isspace())
False
>>> print('     '.isspace())
True

2. ใช้ Regex ^\s+$

>> import re
>>> bool(re.match(r'^\s+$', '  abc'))
False
>>> bool(re.match(r'^\s+$', '      '))
True