Python - 成语来检查字符串是否为空,打印默认值 [英] Python - Idiom to check if string is empty, print default
本文介绍了Python - 成语来检查字符串是否为空,打印默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道,有没有一个Python成语来检查字符串是否为空,然后打印一个默认值(如果是)?
(上下文是Django for UserProfile的 __ unicode __(self)
函数 - 基本上,我想打印名字和姓氏,如果它存在,然后用户名,如果他们不' t都存在)
干杯,
维克多
解决方案
pre> displayname = firstname + lastname或username
名字和姓氏有0个长度的空白字符串
I'm just wondering, is there a Python idiom to check if a string is empty, and then print a default if it's is?
(The context is Django, for the __unicode__(self)
function for UserProfile - basically, I want to print the first name and last name, if it exists, and then the username if they don't both exist).
Cheers, Victor
解决方案
displayname = firstname + lastname or username
will work if firstname and last name has 0 length blank string
这篇关于Python - 成语来检查字符串是否为空,打印默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文