in this post, We’ll learn how to combine two or more strings in python. You can also call this Python String Concatenation, or python string add. The String Concatenation allows us to join two strings together.
There are various techniques to concatenate two or more strings. The “+”, “%” operator, and the join() and Numpy add()
method can all be used to concatenate strings. You can also new f-string method to concatenate string.
This article will go over the five most often used Python string concatenation methods and give examples of how to use each one.
We’ll cover the following topics in this tutorial:
- Concatenate strings using the Python “+” operator
- How to concatenate string and int in python
- String concatenation with Join() Function
- Concatenate strings using the Python “%” operator
Types of strings in Python
There are two types of python strings: basic strings and Unicode strings.
Basic Strings
Basic strings are an array of 8-bit bytes. Each single byte represents each character in a string, and a character is represented by each byte.
A single character in Python is represented as a single character string.
str = 'Hi, i am pythonpip string'
Unicode Strings
Unicode strings are stored as an array of 16-bit bytes. Unicode strings are useful for Chinese and Japanese. Unicode strings start with the “u” character.
str = u'Hi, i am pythonpip string'
Combine Two or More string in Python
Let’s combine two strings in python 3 using different methods:
Concatenate strings using the Python “+” operator
We can combine two or more strings together with Plus (+
) operator. its a very and easy to concatenate string. the “+” character is used to add a variable to another variable:
fname = "Lin " lname = "Dan" name = fname + lname print(name)
Output:
Lin Dan
in the above code, we have added fname and lname and stored them into the name variable.
How to concatenate string and int in python
We can’t concatenate one string with an int type using the +
operator. A string cannot be concatenated with a non-string type. it will throw “TypeError: unsupported operand type(s) for +: ‘int’ and’str'”.
First, we’ll convert int type to str
then concatenate string.
intro = "My, Age is :" age = 43 desc = intro + str(age) print(desc)
Output:
My, Age is :43
we have used str()
method to concatenate one string and an int type.
String Concatenation With Join() Function
We can also use join() method to concatenate two strings in python. We can also combine multiple strings together as like + operator.
lang = ["Reactrjs", "Python", "Nodejs"] desc = 'My skills are: ' print(desc + ', '.join(lang))
Output:
My skills are: Reactrjs, Python, Nodejs
Python concatenate strings array Using Numpy
We can also concatenate two string arrays in python, we just need to import NumPy package and use add() method.
import numpy as np types = np.array(['FrontEnd', 'Backend', 'API']) lang = np.array(["Reactrjs", "Python", "Nodejs"]) combi_array = np.char.add(types, lang) print(combi_array)
types and lang are two strings arrays that will get concatenated using np.char.add() method.
Output:
['FrontEndReactrjs' 'BackendPython' 'APINodejs']
Concatenate strings using the Python “%” operator
The Python % operator can also be used to concatenate the strings. This operator can also be used to format the string.
fname = 'Lin' lname = 'Dan' print('Hi, %s %s' % (fname, lname))
Output:
Hi, Lin Dan