Python
Python
Python
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.
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.
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.
INPUT
a = "Apple"
b = "Shake"
print(“% s % s” % (a, b))
OUTPUT
Apple Shake
5. Using the format() function :
INPUT
a = “Independence" b = “Day"
print(“{} {}”.format(a, b))
OUTPUT
Independence Day
6. Using the f-string :
They contain an f at the beginning and curly braces that contain the
expressions.
INPUT
OUTPUT
Moscow Mule
7. Using StringIO :
INPUT
•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.
INPUT
a = "Artificial "
b = "Intelligence"
a += b
print(a)
OUTPUT
Artificial Intelligence
2. Concatenate strings and numbers :
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.
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 :
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
https://www.knowledgehut.com/blog/pr
ogramming/concatenate-strings-python