[PYTHON] Concatenation of ruby strings

About articulation

Surprisingly, ruby has a string concatenation with a grammar similar to C.

p( "x" "Y" ) #=> "xY"
p( "x" 'Y' ) #=> "xY"
p( 'x' 'Y' ) #=> "xY"

Since String # + is (I think) a method call no matter what, string concatenation is meaningful in terms of runtime efficiency. That's not the case with Java or (probably) perl.

so.

You can't write parentheses because the result of the concatenation is a literal, and the concatenation itself is not an operation.

p( ( "x" )"Y" ) #=> SyntaxError: unexpected ')',The following is omitted

This area is the same as C / C ++.

Also, since concatenation is not an operation, it has higher precedence than any operator.

p( ! "x" "y" ) #=> false
p( "x" "y".size ) #=> 2
p( { "x" "y"=>"a" "b" } ) #=> {"xy"=>"ab"}

This point is the same as C / C ++.

Various literals

Unlike C / C ++, ruby has various strings.

p( ?x 'Y' ) #=> "xY"
p( 'x' ?Y ) #=> SyntaxError: unexpected ')', expecting ':'
p( %Q!x! "Y" ) #=> "xY"
p( 'x' %Q!Y! ) #=> SyntaxError: unexpected tFID,(The following is omitted)

It depends on the order. If there is a ? Or % after the string, it will look like an operator.

The same applies to here documents

p( <<"HOGE" "Y" ) #=> "x\nY"
x
HOGE

p( <<HOGE "Y" ) #=> "x\nY"
x
HOGE

p( "x" <<"HOGE" ) #=> "xHOGE"
Y
HOGE
#=>NameError with Y and HOGE: uninitialized constant

p( "x" <<HOGE ) #=> NameError: uninitialized constant HOGE
Y
HOGE
#=>NameError with Y and HOGE: uninitialized constant

For Groovy

By the way. In groovy, writing something that looks like a string concatenation is a method call.

"print" "x" //Output "x"

For Python

Thanks to ciel, I found out that python also has a connection, so I investigated it.

#coding:utf-8
from pprint import pprint

#Double quotes, single quotes, and triple quotes are connected without distinction.
pprint( "x" "Y" ) #=> 'xY'
pprint( "x" 'Y' ) #=> 'xY'
pprint( "x" """Y""" ) #=> 'xY'
pprint( """x""" 'Y' ) #=> 'xY'
pprint( '''x''' """Y""" ) #=> 'xY'

#u of prefix is valid if it is attached to any one.
#C required for all/C++Is different.
pprint( u'x' u'Y' ) #=>u'xY'
pprint( u'x' 'Y' ) #=>u'xY'
pprint( 'x' u'Y' ) #=>u'xY'
pprint( u'x' Ur'Y' ) #=>u'xY'
pprint( 'a' "b" '''c''' """d""" u'e' "f" '''g''' """i""" ) #=>u'abcdefgi'

#R in prefix is valid only for those with.
# C++I think it works the same as R in 11.
pprint( '\x78' '\x59' ) #=>'xY'
pprint( R'\x78' '\x59' ) #=>'\\x78Y'
pprint( '\x78' R'\x59' ) #=>'x\\x59'
pprint( uR'\x78' R'\x59' ) #=>u'\\x78\\x59'
pprint( r'\x78' u'\x59' ) #=>u'\\x78Y'

I was surprised that python has string concatenation in the first place. It's even more surprising that the prefix ʻu` should be attached to one of them.

For D language

Since D language is strongly influenced by C / C ++, I thought that there would be some concatenation anyway, and I found it. But when I thought it would be the same rule as C / C ++, it was a little different. For more information http://www.kmonos.net/alang/d/lex.html checking ...

To quote the important point:

Strings that are connected by the operator ~ or that are simply written side by side are combined:

And that. However, there is a difference between connecting with ~ and connecting with a blank.

  writeln("Hel" "lo W" "orld!".length); //=> 12
  writeln("Hel"~"lo W"~"orld!".length); //=>error

Will be.

Loose recruitment: Languages with concatenated strings (other than C / C ++ / ruby / python / D)

Reference article

tbpgr's "Ruby | Standard and non-standard binding methods for string literals" http://qiita.com/tbpgr/items/08014f867f7d8cc1200f

Recommended Posts

Concatenation of ruby strings
00. Reverse order of strings
Replacement of strings containing Linux spaces
[Excel] Addition of character strings (combination)
Change the length of Python csv strings
Bulk replacement of strings in Python arrays
[Python] Various combinations of strings and values