Appending Multiple Strings can be done by the below methods:
- Using f-string method
- Using string.format() method
- Using ‘+’ Operator
- Using ‘%’ Operator
NOTE:
You can try all the below examples for N- number of strings. Not only for 3 strings.
1)Using f-string method
PEP 498 created a new string formatting mechanism known as Literal String Interpolation, or f-strings (because of the leading f character preceding the string literal). The goal of f-strings is to make string interpolation easier.
Prefix the string with the letter ” f ” to make an f-string. The string itself can be formatted in the same way that str.format() does. f-strings are a simple and convenient approach to format python expressions by embedding them inside string literals.
The Python f-string is mostly used for string formatting.
Syntax:
f '{string_1} {string_2} {string_N}'
The interpolated string is placed between the curly braces{}.
Example
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Give the third string as static input and store it in another variable.
- Concatenate(append) all the given three strings using the f-string method and store it in another variable.
- Print the result string after appending the given three strings using the f-string.
- The Exit of the Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = 'Hello' # Give the second string as static input and store it in another variable. gvn_scndstr = 'this is' # Give the third string as static input and store it in another variable. gvn_thrdstr = 'Btechgeeks' # Concatenate(append) all the given three strings using the f-string method concat_str = f'{gvn_fststr} {gvn_scndstr} {gvn_thrdstr}' # Print the result string after appending the given three strings using # the f-string. print("The result Concatenated string is = ", concat_str)
Output:
The result Concatenated string is = Hello this is Btechgeeks
2)Using string.format() method
The Python string.format() function can also be used to efficiently format strings.
Syntax:
For only one string
{ }.format(str)
For multiple strings
{ } { }.format(str_1, str_2)
The string.format() function formats strings and aids in string substitution using positional formatting, i.e. according to the position of the string in the function argument list.
Example:
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Give the third string as static input and store it in another variable.
- Concatenate(append) all the given three strings using the format() method by passing the given 3 strings as the arguments to it.
- Store it in another variable.
- Print the result string after appending the given three strings using the format method.
- The Exit of the Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = 'Hello' # Give the second string as static input and store it in another variable. gvn_scndstr = 'this is' # Give the third string as static input and store it in another variable. gvn_thrdstr = 'Btechgeeks' # Concatenate(append) all the given three strings using the format() method # by passing the given 3 strings as the arguments to it. concat_str = "{} {} {}".format(gvn_fststr, gvn_scndstr, gvn_thrdstr) # Print the result string after appending the given three strings using # format method print("The Concatenated string using the format method = ", concat_str)
Output:
The Concatenated string using the format method = Hello this is Btechgeeks
3)Using ‘+’ Operator
To join multiple strings together, use Python’s concatenation operator, i.e. the ‘+’ operator.
Syntax:
str_1 + str_2 + ..... + str_N
Example
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Give the third string as static input and store it in another variable.
- Concatenate(append) all the given three strings using the ‘+’ operator and store it in another variable.
- Print the result string after appending the given three strings using the ‘+’ operator.
- The Exit of the Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = 'Hello' # Give the second string as static input and store it in another variable. gvn_scndstr = ' this is' # Give the third string as static input and store it in another variable. gvn_thrdstr = 'Btechgeeks' # Concatenate(append) all the given three strings using the '+' operator # and store it in another variable. concat_str = gvn_fststr+gvn_scndstr+gvn_thrdstr # Print the result string after appending the given three strings using # the '+' operator. print("The Concatenated string using the '+' operator = ", concat_str)
Output:
The Concatenated string using the '+' operator = Hello this isBtechgeeks
4)Using ‘%’ Operator
Example
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Give the third string as static input and store it in another variable.
- Concatenate(append) all the given three strings using the ‘%’ operator and store it in another variable.
- Print the result string after appending the given three strings using the ‘%’ operator.
- The Exit of the Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = 'Hello' # Give the second string as static input and store it in another variable. gvn_scndstr = 'this is' # Give the third string as static input and store it in another variable. gvn_thrdstr = 'Btechgeeks' # Concatenate(append) all the given three strings using the '%' operator # and store it in another variable. concat_str = "%s %s %s" % (gvn_fststr, gvn_scndstr, gvn_thrdstr) # Print the result string after appending the given three strings using # the '%' operator. print("The Concatenated string using the '%' operator = ", concat_str)
Output:
The Concatenated string using the '%' operator = Hello this is Btechgeeks