Write the usage of + and * with strings with an example
Answers
Answered by
21
When '+' is used between two or more strings, the resultant value will be a single string combined.
Example:
>>> print("Hello" + "World")
HelloWorld
Here, we gave two strings "Hello" and "World", and the result was 'HelloWorld' which is a single string combined.
'*' however cannot be used between two strings. One part will need to be a string, while the other, a number. This results in the string being repeated that number of times.
Example:
>>> print("Hello"*3)
HelloHelloHello
The string we gave was 'Hello' and the number, 3. The string was repeated 3 times.
Hence,
- '+' can be used when you want to combine strings into one.
- '*' can be used when you want to repeat a string 'n' number of times.
Similar questions