Python concatenate strings – basics
Strings in Python are an array of bytes representing Unicode characters. In Python, strings are surrounded by double or single quotation marks. Unlike other programming languages, however, Python does not have a character data type.
Python concatenate strings – The join() method
String concatenation involves combining two strings together. There are multiple ways that we can use concatenate strings in Python. In this article, we will explore the three major ones. These include using the addition operator +, the multiplication operator * or using the join() method.
Using the + operator
It is one of the easiest methods that we can use to concatenate more than two strings. However, using this method we can only concatenate strings together, not any other data type. To demonstrate this we are going to create two variables each assigned to a string. We can concatenate the two together as shown below.
Concatenate empty spaces
Although we have managed to concatenate the two strings you’ll notice that the two are actually together side by side. However, sometimes we want a space between the two perhaps for readability. In that case, we are also going to all and empty space between the two strings as shown below.
Concatenate two strings directly
We can also opt to concatenate the two strings directly therefore there is no need to assign them to variables first. This will also work just fine and will return the same result.
Python concatenate strings –TypeErrors
As we mentioned in the beginning we can only concatenate strings together. This means that although the + operator is also used with numeric types in Python, we cannot use it to concatenate a string and any numeric type. In the example below we are trying to concatenate an integer and a string, this results in a TypeError.
Python concatenate strings – The str() function
However, if we really wish to concatenate a numeric type and a string, then in that case we have to convert the numeric type into a string using the str() function.
Python concatenate strings – Enclose integers and floats in quote marks
Alternatively, we can also opt to enclose the integer or float that we want to concatenate in double or single quote marks. This will make the interpreter consider this integer as just another string.
Python concatenate strings – Using the * operator
In Python, this is referred to as the multiplication operator. However, its behaviour varies depending on the data type of the operands. When used with strings this operator appends the same string to itself according to the number of times specified.
We can do this as many times as we wish the string to be repeated.
Using the .join() method
The join() method takes all items in an iterable and joins them together into one string.
We can also specify a separator that will separate the items when they are joined together.
In the example below we have created a list of animals, we are going to use the .join() method to join these strings into one.
We can also join the string items together without specifying any separator and instead have just a blank space that will concatenate them together without any separator as shown below.
Python concatenate strings – Iterating with loops
We can also opt to use a loop to iterate through the list and join the items together, however, the .join() method is more precise.
Summary
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
- How to use Python decorator
- How to use the Python argparse module
- How to use Python queue
- How to use Python threading
- How to concatenate strings in Python
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
