Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

A Guide on Python String Concatenation [with Examples]

Updated on 25 October, 2024

74.94K+ views
12 min read

The string data type in Python is a very important building block of programming. It is basically a sequence of one or more characters that represent Unicode characters. The characters could be letters, numbers, or symbols. The strings are immutable in nature, which means they are unchanging. Click here to learn more about sys.argv command line arguments in Python. 

You can implement string formatting techniques in Python, like Concatenate Strings Using Python or splitting strings in Python. When you merge or combine two or more strings in Python, it is called string concatenation.

In this article, we will understand what concatenation is and its importance. We will delve into different ways of concatenating strings, including the + operator, * operator, and % operator, and take you through various concatenation methods, including the join() method, format() function, the f-string, and StringIO methods. 

What is Python String Concatenation?

String Concatenation in Python is the operation of joining character strings end-to-end. If you have just started working on Python, you might come through a time when you will need to merge or combine the contents of two or more strings together. In technical terms, this merging or combining of strings together into a single string is called String concatenation. 

The simplest way to explain concatenation in Python is when you take two separate strings stored in the Interpreter and combine them so that they become one single string. 

For example, if you take one string as “foot” and another string as “ball” and then merge them using the concatenation technique, it comes out to be a single string “football”. 

There are several ways in which you can perform string concatenation in Python. However, the simplest method is using the “+” operator. To learn more about string concatenation in Python, explore the best Data Science course in India today.

How to Concatenate Strings in Python Using Different Ways?

Python comprises a number of ways when it comes to concatenating or combining strings together. Since Python is an object-oriented programming language, everything in Python is an object. So, the new string that is created after concatenation is also referred to as a string object in Python.  

Let us see what the different ways by which we can concatenate strings in Python are. 

  1. Using the + operator 
  2. Using the * operator
  3. Using the join() method
  4. Using the % operator
  5. Using the format() function
  6. Using the f-string
  7. Using StringIO

Using the + operator 

The simplest and most common method of concatenating a string is using the plus symbol (“+”). Let us see an example to understand it better: 

a = “Python” 
b = “is” 
c = “cool” 
print(a + b + c) 

Pythoniscool

Here, we have declared three string variables, “a”, “b,” and “c,” with three different string values. Then, we concatenate the three strings with the help of the “+” operator and display the output using the print statement. The output is the combination of the three strings together.  

You might use the “+” operator when you have a few strings to concatenate. This is because strings are immutable i.e. they cannot be changed once created. So, for each concatenating statement, the interpreter creates a new object. Thus, it will be quite inefficient if you try to concatenate many strings using the “+” operator. 

Another disadvantage of the “+” operator is that it does not allow any separator or delimiter between the strings. If you want to concatenate “Hello” and “World” with whitespace as a separator, you need to do something like this “Hello” + “ ” + “World,” and the output will be “Hello World”. Data science bootcamps are the best way to kickstart your Python programming journey with a lot of hands-on exercises.

Using the * operator 

The asterisk (*) operator is used repeatedly when you want to concatenate the same string. For example, if you have a " red " string and want the same string to be concatenated three times, you use the * operator. The result will be “redredred”.  

An example to illustrate the concatenation of string using the “*” operator: 

a = "Python"  
print(a * 3) 

PythonPythonPython 

Here, we have declared a single string variable “a” with a string value. Then, we concatenate the string with the help of the “*” operator and display the output using the print statement. The output combines the string with the same string three times repeatedly. 

If you aspire to be a Python programmer, consider taking up the Executive PG Programme in Data Science & Machine Learning from University of Maryland by upGrad, an excellent starting point for any Python programmer.

Using the join() method 

The join() method is the most flexible way of concatenating strings in Python. If you have many strings and you want to combine them together, use the join () method. It is a string method, and the most interesting thing about join() is that you can combine strings using a separator. It works on iterators like lists, tuples, strings, dictionaries, etc.  

An example to illustrate the concatenation of string using the “*” operator: 

a = "Welcome" 
b = "to" 
c = "Python" 
print(“-”.join([a,b,c])) 

Welcome-to-Python 

Here, we have declared three string variables, “a”, “b,” and “c,” with three different string values. Then, we concatenate the three strings with the help of the join() method with “-” as a separator and display the output using the print statement. The output is the combination of the three strings together with the dash (“-”) operator in between the strings. 

Using the % operator 

The modulus operator (“%”) can be used for string formatting and concatenation. It is useful for cases where you must combine strings and perform basic formatting. 

An example to illustrate the concatenation of string using the “%” operator: 

a = "Apple"  b = "Shake"  print(“% s % s” % (a, b)) 

Apple Shake 

Here, we have declared two string variables, “a” and “b,” with two different string values. Then, we concatenate the two strings with the help of the (“%”) and display the output using the print statement.  

The “% s” denotes the string data type in Python, and the modulus (“%”) operator combines the string stored in the two variables “a” and “b”. The string value in the variables is passed to the string data type, and the output is displayed as the combination of two strings.

Using the format() function 

The str.format() function is a powerful function in Python that is also used for both String formatting and String Concatenation. This function combines different elements within a string through positional formatting.     

An example to illustrate the concatenation of string using format() function: 

a = "Virgin" 
b = "Mojito" 
print(“{} {}”.format(a, b)) 

Virgin Mojito 

Here, we have declared two string variables, “a” and “b” with two different string values. Then, we concatenate the two strings with the help of the format() function and display the output using the print statement.  

The curly braces (“{}”) used here are used to fix the string position. The first variable is stored in the first curly braces and the second one in the second curly braces. The job of format() function is to concatenate the strings stored in variables “a” and “b” and display the combined string. 

Using the f-string  

Formatted string literals or f-strings, in short, are string literals in Python. They contain an f at the beginning and curly braces that contain the expressions. It calls the str() method when an object argument is used as field replacement. 

Let us see an example to illustrate the concatenation of string using f-string:

a = "Moscow" 
b = "Mule" 
print(f’{a} {b}‘) 

Moscow Mule 

Here, we have declared two string variables, “a” and “b,”with two different string values. Then, we concatenate the two strings with the help of the f-string and display the output using the print statement.  

The f-string expressions are evaluated at runtime, and they are formatted using the __format__ protocol in Python. It is considered a cleaner and easier way of concatenating strings in Python compared to the format() function.

Check out our free courses to get an edge over the competition.

Using StringIO 

String concatenation using StringIO is also a very flexible way of combining different strings in Python. In this method, we have to import the StringIO() function from the IO module.  

An example to illustrate the concatenation of string using StringIO:

from io import StringIO 
a = StringIO() 
a.write(“Machine ”) 
a.write(“Learning”) 
print(a.getvalue()) 

 

Machine Learning 

Here, we have declared two string variables, “a” and “b” with two different string values. Then, we concatenate the two strings with the help of the StringIO() imported from the IO module and display the output using the print statement.  

Here, the variable “a” acts as a file object in Python. The write() function is used here to write the string to the file, and the getvalue() function returns the entire content of the file.

Miscellaneous concatenations in Python 

We have covered all the ways by which we can concatenate different strings in Python. Let us see a few more miscellaneous examples to understand String Concatenation better. 

1. Concatenate multiple strings 

There are various ways by which you can concatenate multiple strings in Python. The most common among them is using the plus (“+”) operator. You can combine both string variables and string literals using the “+” operator. 

However, there’s another method that allows an easy way of concatenating multiple strings. It uses the in-place (+=) operator. The in-place operator concatenates the sequence with the right operand, and the result gets assigned to that sequence. 

Let us see an example of string concatenation using the (“+=”) operator:

a = "Artificial " 
b = "Intelligence" 
a += b 
print(a) 

 

Artificial Intelligence 

Here, two string variables, “a” and “b” are declared with two different string values. The string on the right side of the “+=” operator is combined with the string variable on the left side. Then, the output is displayed using the print statement.  

You can also add a string to the end of a string variable using the “+=” operator: 

a = "Basket" 
a += "ball" 
print(a) 

Basketball 

Another way of concatenating multiple strings in Python is just by writing string literals consecutively: 

a = "Red""Green""Blue" 
print(a) 

RedGreenBlue

2. Concatenate strings and numbers 

There are numerous ways of concatenating strings in Python. However, not all methods can concatenate strings and numbers. If you use the “+” operator to combine strings and numbers, it will raise errors. This is because strings can hold any recorded characters, but numbers like integers or floats are recorded number values. 

a = "Rolls Royce "  b = 1948  
print(a + b) 

The error shows that the interpreter can concatenate a string value with another string value but cannot concatenate a string value with an integer. Although, you can overcome this problem with the help of the str() function in Python. It converts any integer or floating-point number into a string.  

Let us see the same example with the str() function:

a = "Rolls Royce " 
b = str(1948) 
print(a + b) 

 

Rolls Royce 1948 

The str() function converts the integer value 1948 into a string, and then it is concatenated with variable “a,” and the output is displayed using the print statement. 

You can also use the format() function when you need to convert a number with decimal places or zero padding.

3. Concatenate a list of strings into one string 

You can concatenate a list of strings into one string using the join() method. It takes a character as a delimiter string. If you use an empty string as the delimiter, the list of strings will be simply concatenated without any separator.  

Let us see an example of concatenating a list of strings using the join() function:

a = ["Apple", "Orange", “Banana”, “Mango”] 
print(“\n”.join(a)) 

 

Apple 
Orange 
Banana 
Mango 

Here, the variable “a” is a list declared with four different string values. We have used newline (“\n”) as the delimiter in the join() method, which inserts a newline for each of the strings. 

The output is the four strings, with each string in a new line. 

You can use any other delimiter like comma (,) or hyphen (-) in the join() method and then perform concatenation. Also, note that thejoin() method can also concatenate other iterators like tuples, sets, dictionaries, etc.

4. Concatenate a list of numbers into one string 

Python does not allow the concatenation of strings with numbers or numbers with numbers. However, you can convert a numeric value into a string using the str() method and then perform concatenation. 

If you want to combine a list of numbers into one string, you first need to convert each integer in a list to a string using the str() function. Then, combine all the converted strings into a single string with the join() method.

Let us see an example to understand it better: 

a = [1, 2, 3, 4, 5] 
b = [str(a) for a in a] 
print(“;”.join(b)) 
1;2;3;4;5 

 

Here, the variable “a” is a list declared with five integer values. We convert each of the integers into a string using the str() function and store it in variable “b”. Then, we combine them together using the join() method with a colon (;) as the delimiter.

Here, the variable “a” is a list declared with five integer values. We convert each of the integers into a string using the str() function and store it in variable “b”. Then, we combine them together using the join() method with a colon (;) as the delimiter.

What is the Need for String Formatting in Python? 

String formatting in Python is a robust and important part of the toolkit of any Python programmer. String formatting techniques have greatly evolved since the time Python was developed. Almost every piece of production software created has its advantage in some way or the other.  

Formatted strings in Python are evaluated at run time which acts as a basic capability of any high-level language. String concatenation using the “+” operator at a basic level might seem inefficient and difficult to make expressive. This is where Python’s string formatting starting from the “%” formatting to the format() method, comes into action. They exhibit great potential when it comes to crafting strings. 

Some Useful Tips on Concatenation 

Now let me give you some useful tips on how to concatenate strings using Python:

  • The string-formatting operator “%” is a potentially fast and suitable operator when you need to concatenate a few pieces of string. Also, you don’t need to call the str() function when combining numbers because this operator does it implicitly. It also enhances the readability of the code. 
  • The join() method is the fastest, cleanest, most elegant, and most readable method when you need to concatenate many small pieces of string into a larger string. 
  • When you have many small pieces of strings that come either from input or computation and are not in a sequence, always use a list to contain the strings. You can use list comprehension or the append method in Python to arrange your list in a sequence.  

Conclusion 

Let us summarize what we have learned in this article so far:

  • Concatenation and its importance. 
  • Different ways of Concatenate Strings Using Python.
  • Some miscellaneous concatenation methods. 
  • Important tips on concatenating strings. 

Concatenation is a crucial part of String manipulation in Python. There are numerous ways to perform concatenation. However, some are more useful than others in some cases. Now that you have quite an experience in concatenating strings, you can look out for other string formatting methods that Python provides, or you can check out the PEP article on Advanced String Formatting on Python.org for more information. 

So why not start your journey with the Python Programming Bootcamp from upGrad? You can learn Python, SQL, and other programming tools like NumPy, Pandas, and more with live online classes over eight weeks. 

Elevate your data science expertise with our top certifications. Discover the programs below to start your journey

Gain essential data science skills with our expert-led courses. Browse below to start learning today

Stay informed with our top data science articles. Dive in to explore insights, career tips, and industry trends