String Concatenation

 

Python

Java

 

The concatenation operator + joins together two strings to form a third, new string.  Operands of other types must be converted to strings before they can be concatenated.

 

Examples:

 

"35" + " pages long."

str(35) + " pages long."

 

If x and y are any objects, the code

 

str(x) + str(y)

 

concatenates their string representations.

 

 

The concatenation operator + joins together two strings to form a third, new string.  If one of the operands is a string, the other operand can be of any type.

 

Examples:

 

"35" + " pages long."

35 + " pages long."

 

Any non-numeric object can also be used in a string concatenation, because all Java objects recognize the toString() method.  This method returns the name of the objectÕs class by default, but can be overridden to return a more descriptive string.  Thus, if x and y are any objects, the code

 

x.toString() + y.toString()

 

or

 

x + y

 

concatenates their string representations.

 

 

 

 

Previous

Index

Next