DO you ever combined the python strings?
I'm sure you have. But you've only used the one or two methods.
In this blog post, I'm going to show you the 3 methods to combine the python strings.
This 3 methods empower your python program.
Hence, I want you to use it to boost your python.
So, let's dive into it.
## 1. Concatenate(combine) using + operator
First method is using + operator.
The + operator concatenate the two similar data type. Here, I'm going to use the string data type.
Because we've to concatenate the string.
So, when you concatenate the strings your return value is string.
When above code run it gives you,
Here, you can see that new string Hello world! has came.
Likewise, you can concatenate the multiple strings.
After running the code you'll get,
So, here you've seen the concatenation of multiple strings.
What happens when you concatenate the different data types?
When you run above code you'll get TypeError:
Here, you can see that TypeError has come that means you can't concatenate the different data types.
## 2. Concatenate using ,(comma) operator
The second method is using ,(comma).
So, the output will be same as 1st method.
But the benefit is here you can concatenate the two different data types. That means you can concatenate the string with integer.
When you run above code, you'll get---->
Here, you can see that the string has concatenated with integer.
It hasn't given the error.
So, that's the vitality of python string.
Whereas, when I've concatenate the different data type. It has given the type error.
Thus, ,(comma) has benefit over +operator in string concatenation. Great thing is that very few programmer know it. That's why very few use it.
If you want to concatenate the string and the integer and need string as return value. then you can use it.
##3. Concatenate using
Third method you can use to concatenate is Python fstring.
In this method, you've to use variable name inside the curly braces. This method, format the string in easy way.
It has introduced in python3.x by eric in 2015.
Let's see it with following example:--->
After, the code will run it gives the output:--->
Now, you can see that the output is same as the previous one.
So, this is the third method you can use to combine the python string.
Finally, you've seen the three methods for string concatenation.
First, method you've seen the +operator.
Second, you've seen that ,(comma) to concatenate the python strings. Also, it concatenates the different data types.
The third method is fstring method.
Now, with this method you can format the multiple strings.
So, what do you think about it? Is anything you want to cover? comment on it!!!
Also, Do this copy is good for you?
If you feel it
Subscribe to my newsletter. so, you can grab the fresh python topics.
Did you find this article valuable?
Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!