代码

  • 变量为字符串类型(优雅的方式)
1
2
3
4
if not string:
print('not empty')
else:
print('empty')
  • 变量类型不确定
1
2
3
4
if string == '':
print('not empty')
else:
print('empty')

参考

  1. https://docs.python.org/2/library/stdtypes.html#truth-value-testing
  2. http://stackoverflow.com/questions/9573244/most-elegant-way-to-check-if-the-string-is-empty-in-python

留言