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

วิธีเช็คว่า String มีเฉพาะ Uppercase Letters ใน Python

1. ใช้ isupper()

>> 'Hello world'.isupper()
False
>>> 'HELLO'.isupper()
True

2. ใช้ Regex ^[A-Z]+$

>> import re
>>> bool(re.match('^[A-Z]+$', '123aAbc'))
False
>>> bool(re.match('^[A-Z]+$', 'ABC'))
True