The concatenation of two strings has been discussed in a variety of languages. In Python, however, adding one string to another is a simple job.
The ability to execute this operation has a wide range of applications. Let’s look at some examples of how this can be done.
Types of strings in Python
Python strings are divided into two categories: basic strings and Unicode strings.
Basic Strings
Strings are made up of an array of 8-bit bytes. Each character in a string is represented by a single byte, and each byte represents a character.
In Python, a single character is represented by a single character string.
str = 'Hi, i am pythonpip string'
Unicode Strings
Unicode strings are saved as a 16-bit byte array. For Chinese and Japanese, Unicode strings come in handy. Unicode strings begin with the letter "u
.”
str = u'Hi, i am pythonpip string'
Combine Two or More string in Python
Let’s use different methods to merge two strings in Python 3:
Method #1 : Using += operator
This operator can be used to perform this specific task of string concatenation. This is more easier than the more typical approaches used in other languages, such as using a specialised function to execute this work.
fname = "Lin " lname = "Dan" name = fname + lname print(name)
Output:
Lin Dan
Method #2 : Using join()
The join function can also be used to execute string concatenation. When we have more than two strings to concatenate, this method outperforms the previous method.
lang = ["Reactrjs", "Python", "Nodejs"] desc = 'My skills are: ' print(desc + ', '.join(lang))
Output:
My skills are: Reactrjs, Python, Nodejs
Method #3 : “%” 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