开发者

Is There a Way to Return a String Concatenation in Python?

I've been using Perl for some time and have gotten used to the syntax:

return "$var1$var2";

for easily returning a concatenation of two strings in one step. Is there a way to do something similar in Python? I'd love to avoid doing it in two开发者_运维技巧 steps, if possible.


Simple:

>>> "a" + "b"
'ab'
>>> "%s%s" % ("a", "b")
'ab'
>>> "{a}{b}".format(a="a", b="b")
'ab'
>>> "{}{}".format("a", "b")
'ab'
>>> "{0}{1}".format("a", "b")
'ab'
>>> "a" "b"
'ab'
>>> "".join(("a", "b"))
'ab'


I don't see how addition is two steps.

return var1 + var2


just use +.

def f():
    a = 'aaa'
    b = 'bbb'
    return a + b
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜