How to Two Strings Concatenate in Python
Here, you will know and get the example code of Two Strings Concatenate in Python using 5 different ways.
What is string in python?
Python strings are sequences of characters, which are immutable. They are typically used for storing text or sequence data. Python strings are often enclosed in single or double quotation marks. They’re frequently used to represent text values like words and sentences.
Different 5 Ways to perform Two Strings Concatenate in Python
String concatenate is a way to add two or more strings together. Here we are using 5 different following ways to do the concatenation:
- Using + operators
- Using join() method
- Using % method
- Using f-string
- Using format() function
using + operator
1 2 3 4 5 | str1 = "Code" str2 = "Revise" str3 = str1 + ' ' + str2 print(str3) |
Output
Code Revise
using join() method
1 2 3 4 | str1 = "Code" str2 = "Revise" print(" ".join([str1, str2])) |
Output
Code Revise
using % method
1 2 3 4 | str1 = "Code" str2 = "Revise" print("%s %s" %(str1, str2)) |
Output
Code Revise
using f-string
1 2 3 4 | name1 = "Code" name2 = "Revise" f"{name1} {name2}" |
Output
Code Revise
using format() function
1 2 3 4 | str1 = "Welcome" str2 = "CodeRevise" print("{} {}".format(str1, str2)) |
Output
Welcome CodeRevise
Check out our other Python examples