开发者

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

开发者 https://www.devze.com 2023-03-12 12:14 出处:网络
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 simila

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

精彩评论

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

关注公众号