
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to do string concatenation without '+' operator in Python?
In this article, we are going to find out how to do string concatenation without plus operator in Python.
The first technique is utilizing the string inbuilt library's join() method. To mix strings with a separator, use this approach. This method produces a string sequence as output.
Python's join() method offers the ability to combine iterable components that have been separated by a string operator. To return a string of iterators with the sequence's items connected by a string separator, use the built-in Python join function.
Example 1
In the example given below, we are taking 2 strings as input and we are concatenating with whitespace using join() method −
str1 = "Welcome" str2 = "Tutorialspoint" print("The first string is") print(str1) print("The second string is") print(str2) concat = " ".join([str1,str2]) print("The concatenated string is") print(concat)
Output
The output of the above example is as follows −
The first string is Welcome The second string is Tutorialspoint The concatenated string is Welcome Tutorialspoint
Example 2
In the program given below, we are taking the same program as above but we are concatenating the input strings with empty space −
str1 = "Welcome" str2 = "Tutorialspoint" print("The first string is") print(str1) print("The second string is") print(str2) concat = "".join([str1,str2]) print("The concatenated string is") print(concat)
Output
The output of the above example is given below −
The first string is Welcome The second string is Tutorialspoint The concatenated string is WelcomeTutorialspoint
Example 3
In the example given below, we are taking the same program as above but we are concatenating using ‘,’ operator −
str1 = "Welcome" str2 = "Tutorialspoint" print("The first string is") print(str1) print("The second string is") print(str2) concat = ",".join([str1,str2]) print("The concatenated string is") print(concat)
Output
The output of the above example is as shown below −
The first string is Welcome The second string is Tutorialspoint The concatenated string is Welcome,Tutorialspoint
Using filter() method
The second technique is to use the format() function from the string library, which is an inherent method. It's mostly used in print statements to include variables. To signal that a certain variable is present, we'll use blossom brackets in double-quotes, and then specify the variable name inside the format() function.
Example
In the example given below, we are taking 2 strings as input and we are concatenating using filter() operation −
str1 = "Welcome" str2 = "Tutorialspoint" print("The first string is") print(str1) print("The second string is") print(str2) concat = "{} {}".format(str1, str2) print("The concatenated string is") print(concat)
Output
The output of the above example is as shown below −
The first string is Welcome The second string is Tutorialspoint The concatenated string is Welcome Tutorialspoint
- Related Articles
- String Concatenation by + (string concatenation) operator.
- How does concatenation operator work on list in Python?
- How does concatenation operator work on tuple in Python?
- MySQL concatenation operator?
- Concatenation of two String Tuples in Python
- Python – Incremental Slice concatenation in String list
- Why should you be careful about String concatenation (+) operator in loops using Java?
- How to perform string aggregation/concatenation in Oracle?
- Equaivalent of Oracle concatenation operator in MySQL?
- String Formatting Operator in Python
- How can we use Python Ternary Operator Without else?
- String Concatenation in Java
- What is the most efficient string concatenation method in python?
- How do we use equivalence (“equality”) operator in Python classes?
- What does the &= operator do in Python?
