Using string operators to concatenate objects in Python

By Abhinash Jena & Priya Chetty on August 22, 2022

The previous article explained the functions involved in manipulating string data in Python, such as extracting or splicing string objects. This article explains the methods of concatenating strings by using string operators. However, it is important to understand the differences in the terms ‘string sequence’ ‘string object’, and ‘string literals’ in Python.

String sequence and string literals are similar

String sequence refers to the entire sequence of characters. However, there is a very similar term, ‘string literal’, which refers to that sequence of characters, present between single or double quotes, which has been given as input. Essentially, string literals and string sequences are similar.

x = 'Hi, how are you?'
print(x)
#OUTPUT
Hi, how are you?

In this example, variable x has a string sequence and string literal, while x is the variable. However, when Python reads the variable x, it stores the information as a string object. When the variable x is printed, the Python Interpreter will not create a new string, but simply consider the reference of the existing string object.

TIP

String objects are immutable and cannot be changed or manipulated over time. To change the string from ‘Hi, how are you?’ to ‘Hi, how have you been?’ reassign variable x to overwrite its contents.

Using the var.replace() method to

Another way to overwrite existing string objects is by using the var.replace() method.

x = ‘Hi, how have you been?’
print(x.replace(“how”, “where”))
#OUTPUT
Hi, where have you been?

Using the string operator ‘+’ for concatenation

The ‘+’ operator in Python can also be used to join string objects, end-to-end, to create a new string object in Python. The act of linking objects in a series is known as concatenation.

a='Bat'
b='man'
print(a+b)
#OUTPUT
Batman

It is also possible to include escape sequences with concatenation. This is especially useful when the sequence has been assigned to a variable. So instead of rewriting the entire string sequence, simply edit the sequence using appropriate escape sequences.

x = 'Dont cry'
print(x+'\beate')
#OUTPUT
Dont create

Concatenating using var.join()

Concatenation can also be done by using the function, str.join(). This function combines two different string objects by passing one string through another.

Suppose, we have a string object, ‘Maryhadalittlelamb’ and we now want to add ‘_’ between each letter.

a = 'Maryhadalittlelamb'
print('_'.join(a))
#OUTPUT
M_a_r_y_h_a_d_a_l_i_t_t_l_e_l_a_m_b

Using the string operator ‘*’ to multiply the object ‘n’ times

The ‘*’ can be used to multiply the string object ‘n’. Whereas n is the number of times the string has to be concatenated.

x = 'Strong '
print(x*10)
#OUTPUT
Strong Strong Strong Strong Strong Strong Strong Strong Strong Strong 

I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.

I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them. 

Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here

I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal. 

 

Discuss