Get The Most Affordable Hosting in the World!
Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.
Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!
Get Started Now!The "+" operator is well-known as an addition operator, returning the sum of two numbers. However, the "+" symbol acts as string concatenation operator in Python. It works with two string operands, and results in the concatenation of the two.
The characters of the string on the right of plus symbol are appended to the string on its left. Result of concatenation is a new string.
It will produce the following output −
String 1: Hello String 2: World String 3: HelloWorld
To insert a whitespace between the two, use a third empty string.
It will produce the following output −
String 1: Hello String 2: World String 3: Hello World
Another symbol *, which we normally use for multiplication of two numbers, can also be used with string operands. Here, * acts as a repetition operator in Python. One of the operands must be an integer, and the second a string. The operator concatenates multiple copies of the string. For example −
>>> "Hello"*3 'HelloHelloHello'
The integer operand is the number of copies of the string operand to be concatenated.
Both the string operators, (*) the repetition operator and (+) the concatenation operator, can be used in a single expression. The "*" operator has a higher precedence over the "+" operator.
To form str3 string, Python concatenates 3 copies of World first, and then appends the result to Hello
In the second case, the strings str1 and str2 are inside parentheses, hence their concatenation takes place first. Its result is then replicated three times.
Apart from + and *, no other arithmetic operator symbols can be used with string operands.
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.