3 useful methods to combine the little python strings

3 useful methods to combine the little python strings

image.png

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.

For example,

Screenshot 2021-11-13 at 09-58-59 Carbon.png

When above code run it gives you,

Screenshot 2021-11-13 at 10-02-51 Carbon.png

Here, you can see that new string Hello world! has came.

Likewise, you can concatenate the multiple strings.

For example,

Screenshot 2021-11-13 at 10-16-33 Carbon.png

After running the code you'll get,

Screenshot 2021-11-13 at 10-17-23 Carbon.png

So, here you've seen the concatenation of multiple strings.

What happens when you concatenate the different data types?

For example,

image.png

When you run above code you'll get TypeError:

Screenshot 2021-11-13 at 10-44-26 Carbon.png

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.

For example,

Screenshot 2021-11-13 at 10-33-48 Carbon.png

When you run above code, you'll get---->

Screenshot 2021-11-13 at 10-34-34 Carbon.png

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
fstring

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:--->

image.png

After, the code will run it gives the output:--->

image.png

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.

Conclusion

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 image.png

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!