How to join two strings to convert to a single string in Python?



A string is a group of characters that can represent a single word or a complete sentence. Unlike in other technologies there is no need to declare strings in python explicitly using a datatype.

Python provides several built-in functions using which we can manipulate strings. A string is an object of the String class, which has multiple methods because everything in Python is an object.

In this article, we are focusing on how to combine 2 strings into a single string in python.

Using the ‘+’ operator

On way to join two strings is using the ‘+’ operator which is also known as the Concatenation operator. This operator combines 2 strings and stores the value in another variable.

The major drawback of the concatenation operator is that it can only be used for strings and cannot be used for other values.

Example

In the example given below, we are taking 2 strings as input and by using the concatenation (+) operator we are combining them.

s1 = 'Welcome to ' s2 = 'Hyderabad' s3 = s1 + s2 print("Combining ",s1,"and",s2) print("Resultant string is",s3) print(s3)

Output

The output of the above program is,

('Combining ', 'Welcome to', 'and', 'Hyderabad')
('Resultant string is', 'Welcome toHyderabad')
Welcome to Hyderabad

Using the join method

The join() method of the string inbuilt library accepts a sequence of values representing the individual strings, combines them and returns the result.

Example

In the program given below, we are using the separator as ‘ ’ and separating the given sequence by that separator using the join method.

separator = " " sequence = ['Hello','how','are','you.','Welcome','to','Tutorialspoint'] res = separator.join(sequence) print("The final sequence is") print(res)

Output

The output of the above program is,

The final sequence is
Hello how are you. Welcome to Tutorialspoint	

Using the format() method

The format() is an inbuilt method from the string library. It is mainly used to incorporate variables in a print statement. We will use flower braces in double quotes to indicate that a specific variable is present and after that, we will mention the variable name inside the format() method.

Example

In the program given below, we are using the join operator to combine 2 strings s1 and s2. Here we are using ‘ ’ as the separator operator.

s1 = 'Welcome to' s2 = 'Hyderabad' s3 = " ".join([s1, s2]) print("Combining ",s1,"and",s2) print("Resultant string is") print(s3)

Output

The output of the above program is,

('Combining ', 'Welcome to', 'and', 'Hyderabad')
Resultant string is
Welcome to Hyderabad

Example

In the program given below, we are using the format() to combine 2 strings, s1 and s2.

s1 = 'Welcome to' s2 = 'Hyderabad' s3 = "{} {}".format(s1, s2) print("Combining ",s1,"and",s2) print("Resultant string is") print(s3)

Output

The output of the above program is,

('Combining ', 'Welcome to', 'and', 'Hyderabad')
Resultant string is
Welcome to Hyderabad

Advertisements