How to concatenate string in Python

How to concatenate string in Python

Satya Python solution , ,

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.

str1="Hello"
str2="world"
print str1
print str2

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 ‘|’.

ary=['hello', 'world']
str='|'.join(ary)
print (str)


Output:

hello|world

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"
joined_str='|'.join(str)
print (joined_str)

Output:

H|a|v|e| |a| |w|o|n|d|e|r| |f|u|l| |d|a|y

You May Also Like..

Multi threading program in python

Multi threading program in python

Multi threading program in python: Multi-Thread is a multiple processes running concurrently in one CPU core processor. It is different […]

Read and write CSV file using python

Read and write CSV file using python : CSV file, which is comma separated file, is the most popular form […]

How to create socket in Python programming

How to create socket in Python programming

How to create socket in Python programming: Socket is network interface, where two nodes in a network connect and communicate. […]

Leave a Reply

Your email address will not be published. Required fields are marked *