Which operator is used to concatenate two strings in Python?
a) +
b) *
c) &
d) ^
Answer:
a) +
Explanation:
In Python, the +
operator is used to concatenate two strings. Concatenation joins two strings end-to-end to create a new string. This operator is widely used for creating messages, building dynamic strings, or combining user inputs.
greeting = "Hello, " + "world!"
# greeting now contains "Hello, world!"
String concatenation is a fundamental operation when working with text data. Python makes it easy to combine strings using the +
operator, which is both intuitive and efficient.
Be careful when using the +
operator, as it only works with strings. Attempting to concatenate a string with another data type, like an integer, will result in a TypeError unless the non-string value is explicitly converted to a string.