Python String Concatenation With Examples

Posted in /  

Python String Concatenation With Examples
vinaykhatri

Vinay Khatri
Last updated on December 5, 2024

    Unlike other programming languages, in Python, we do not represent the string as an array of characters. This is so because Python does not support char data type. Alternatively, to access an individual character from a string, we can use indexing with sq. [] brackets. In this tutorial, we will discuss string concatenation in Python.

    Example

    str1 = "TechGeekBuzz"
    print(str1[0])

    Output

    T

    Python String Concatenation

    String concatenation is a method of combining two or more strings, and in Python, we have various ways to perform string concatenation. These are the four major techniques we can use to perform string concatenation in Python:

    1. + operator
    2. string join() method
    3. % operator.
    4. format() function

    1. Python String Concatenation Using the + Operator

    To add two strings, we can simply use the + or add operator in Python . As its name suggests, it can add - concatenate - two strings and return a copy of the combined string. Using the + operator, we can combine multiple strings together.

    Example

    str1 = "Welcome"
    str2 = "to"
    str3 = "TechGeekBuzz"
    
    combined = str1 + str2 +str3
    
    print(combined)

    Output

    WelcometoTechGeekBuzz

    <Note>: The + operator does not specify space between two strings when performing the concatenation. So we explicitly need to pass the space if we want to have a space between the strings.

    Example

    str1 = "Welcome"
    str2 = "to"
    str3 = "TechGeekBuzz"
    
    combined = str1 + " " + str2 + " " +str3
    
    print(combined)

    Output

    Welcome to TechgeekBuzz

    2. String Concatenation Using the String .join() method

    .join() is a string method that accepts sequential data structure (list, tuple set, and dictionary) as a parameter, joins all its elements together with a string operator, and returns a string value. However, we can pass our own string variables to perform the concatenation operation. While performing the concatenation operation using the join() method, we need to make sure that all the elements present in the sequential object are of the string data type; else, the join() method throws an error.

    .join() Syntax

    "str seperator".join(list)

    Example

    str1= "Welcome to"
    str2= "TechGeekbuzz"
    
    combine1= " ".join([str1,str2])
    combine2 = "-".join([str1,str2])
    
    print(combine1)
    print(combine2)

    Output

    Welcome to TechGeekbuzz
    Welcome to-TechGeekbuzz

    3. Using the % Operator for String Formatting

    This method of string formatting is borrowed from the C programming language . Using the % operator and s (string) code specifier, we set a placeholder for string variables in a string represented by double inverted quotes. This string formatting technique can also be used for string concatenation in Python.

    Example

    str1= "Welcome to"
    str2= "TechGeekbuzz"
    
    combined = "%s %s" % (str1,str2)
    
    print(combined)

    Output

    Welcome to TechGeekbuzz

    Note: Here, the %s signify the string data type.

    4. Python String Concatenation Using the format() Function

    The format() function is generally used for string formatting or passing variable values in a string. Nonetheless, it can also be used for string concatenation.

    Syntax

    "{}".format()

    Example

    str1= "Welcome to"
    str2= "TechGeekbuzz"
    
    combined = "{} {}".format(str1,str2)
    
    print(combined)

    Output

    Welcome to TechGeekbuzz

    Python 3.6 and newer versions also support a new string formatting technique.

    str1= "Welcome to"
    str2= "TechGeekbuzz"
    
    combined = f"{str1} {str2}"
    
    print(combined)

    Output

    Welcome to TechGeekbuzz

    Summary

    Usually, in Python, string contamination is performed using the + operator. Nonetheless, string formatting also provides an alternative approach for string concatenation. The .join() method, on the other hand, joins all the elements of the list together with a string separator.

    People are also reading:

    Leave a Comment on this Post

    0 Comments