How to concatenate string in Python: Concatenating a string is very important feature in any computer programming language. Python provide a very elegant way to join two or more strings into one.
In this post, I will explain how we can join and concatenate two strings into one by using Python methods.
Concatenate operator (+)
In python we can join / concatenate two string by using + operator.
You can join two string “Hello” and “world” to make One string “Hello world” by using + operator.
print str1 + " " + str2
Note: Above print statement work with python version which is below v3. With python version 3 and above you have to put parentheses around print statement. For example print (“Hello world”).
Python Join statement
Python join statement join elements of an array into a string. You can join elements by any charterer (like ‘-‘, ‘|’). Following example demonstrate joining of two element ‘hello’ and ‘world’ by charracter ‘|’.
Join statement with string
In python, you can not only work join with array but also iterate through each character of a string and rejoin it with some special character – pipe, space, tap , etc.
str="Have a wonder ful day"
H|a|v|e| |a| |w|o|n|d|e|r| |f|u|l| |d|a|y