You are on page 1of 20

contents

PAGE NUMBER TOPIC


3 CONCATENATION
4 WHAT IS THE NEED FOR STRING FORMATING IN PYTHON?
5 HOW WE CAN CONCATENATE STRINGS IN PYTHON?
6 USING + OPERATOR
7 USING * OPERATOR
8 USING JOIN() METHOD
9 USING % OPERATOR
10 USING FORMAT() FUNCTION
11 USING THE F- STRING
12 USING STRING IO
13 CONCATENATE MULTIPLE STRINGS
14 CONCATENATE NUMBERS AND RINGS
15 CONCATENATE A LIST OF STRINGS INTO ONE STRING
16 CONCATENATE A LIST OF NUMBERS IN ONE STRING
17 USEFUL TIPS OF CONCATENATION
18 CONCLUSION
19 REFERENCE
20 THANK YOU
 Concatenations :
 Concatenation is an associative operation, so that
the concatenation of three or more strings, for
example , , etc., is well-defined.
 The concatenation of two or more numbers is the
number formed by concatenating their numerals.
 For example, the concatenation of 1, 234, and 5678 is
12345678.
 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”.
 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.
 How can we concatenate
strings in Python?
 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 :

 The simplest and most common method of


concatenating a string is using the plus
symbol (“+”). Let us see an example to understand it
better:

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

 OUTPUT
Pythoniscool
 2. 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:

 INPUT
a = "Python"
print(a * 3)

 OUTPUT
PythonPythonPython
 3. 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:

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

 OUTPUT
 Welcome-to-Python
 4. 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:

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

 OUTPUT
Apple Shake
 5. 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:

 INPUT
a = “Independence" b = “Day"
print(“{} {}”.format(a, b))

 OUTPUT
 Independence Day
 6. 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:

 INPUT

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

 OUTPUT
Moscow Mule
 7. 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:

 INPUT

from io import StringIO


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

•OUTPUT
Machine Learning
 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:

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

 OUTPUT
Artificial Intelligence
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.

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

 OUTPUT
Rolls Royce 1948
 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:

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

 OUTPUT
Apple
Orange
Banana
Mango
 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:

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

 OUTPUT
 1;2;3;4;5
 Some useful tips on concatenation :

 Now let me give you some useful tips on String concatenation


in 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
 Let us summarize what we have learned in this article
so far –
 Concatenation and its importance.
 Different ways of concatenating strings.
 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.
 LEARNING PYTHON, 4TH EDITION BY MARL
LUTZ

 PROGRAMMING PYTHON, 4TH EDITION BY


MARK LUTZ

 https://www.knowledgehut.com/blog/pr
ogramming/concatenate-strings-python

You might also like