首页 > 分享 > python中x y的意思,在Python中x ='y''z'的含义是什么?

python中x y的意思,在Python中x ='y''z'的含义是什么?

If you run x = 'y' 'z' in Python, you get x set to 'yz', which means that some kind of string concatenation is occurring when Python sees multiple strings next to each other.

But what kind of concatenation is this?

Is it actually running 'y' + 'z' or is it running ''.join('y','z') or something else?

解决方案

The Python parser interprets that as one string. This is well documented in the Lexical Analysis documentation:

String literal concatenation

Multiple adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, "hello" 'world' is equivalent to "helloworld".

The compiled Python code sees just the one string object; you can see this by asking Python to produce an AST of such strings:

>>> import ast

>>> ast.dump(ast.parse("'hello' 'world'", mode='eval').body)

"Str(s='helloworld')"

In fact, it is the very act of building the AST that triggers the concatenation, as the parse tree is traversed, see the parsestrplus() function in the AST C source.

The feature is specifically aimed at reducing the need for backslashes; use it to break up a string across physical lines when still within a logical line:

print('Hello world!', 'This string is spans just one '

'logical line but is broken across multiple physical '

'source lines.')

Multiple physical lines can implicitly be joined into one physical line by using parentheses, square brackets or curly braces.

This string concatenation feature was copied from C, but Guido van Rossum is on record regretting adding it to Python. That post kicked of a long and very interesting thread, with a lot of support for removing the feature altogether.

相关知识

在Python中x=y=z的引擎盖下是什么?
Python中is和==的区别详解
若x,y,z均为int型变量, x=(y=(z=10)+5)
表达式:(x>y) ? (z>w) ?x:z:w,(其中x=5,y=
设x,y,z均为int型变量,则执行下列语句后,x,y,z的值分别是x=1;y=
基础python 3D玫瑰花怎么画,给出源代码
【Python】基础
已知变量x与y正相关,变量y,z满足z=
2. global w 3. y = np.sign(np.dot(x
【三分之x=二分之y=四分之z,(xyz不等于零,)二y分之二x加上三y减4z等于多少?】

网址: python中x y的意思,在Python中x ='y''z'的含义是什么? https://m.huajiangbk.com/newsview837544.html

所属分类:花卉
上一篇: [渝粤教育] 西南科技大学 经济
下一篇: VS 2017安装出现的问题:未