Python has powerful built-in capabilities for string manipulation. That’s why web companies like Google love Python—it’s a perfect fit for the text-based web.
This guide shows you how to use string concatenation operators using multiple forms of education:
- Text
- Puzzle
- Video
- Exercise
Ready to learn string concatenation? Let’s get started!
Table of Contents
A Textual Introduction to String Concatenation in Python
There are two basic string manipulation operators.
- The
'+'
operator concatenates two strings. - The
'*'
operator concatenates a string to itself repeatedly.
Those two operators are exemplified in the following example:
>>> 'hello ' + 'world' 'hello world' >>> 'hello ' * 3 'hello hello hello '
The standard arithmetic rules apply to these operators: multiplication first, then addition.
>>> 'hello ' + 'world' * 3 'hello worldworldworld'
Attention: Python’s string type is immutable—meaning that you cannot change an existing string. Instead, Python creates a new string as the result of the string concatenation operators *
and +
.
As a Python professional, you will find yourself using these operators on a daily basis.
Can You Solve This String Concatenation Python Puzzle?
print(3 * 'un' + 'ium')
What is the output of this code snippet?
You can evaluate whether you’ve solved the puzzle correctly, and test your coding skills on our Finxter.com puzzle app: Test your skills now!
Watch the Video About String Concatenation
I found this excellent video on YouTube:
Polish Your Skills With This Interactive Shell Exercise About String Concatenation
Here’s a Python exercise. You have given two variables A
and B
with some string content. Create a short and concise Python statement based on string concatenation to generate the desired output:
Scroll down to see the solution…
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Solution Exercise
The solution is simple: the string concatenation expression 3*(A+B) generates the desired output!
A = 'old mcdonald had a farm\n' B = 'yia-yia-yo\n' print(3*(A+B)) ''' old mcdonald had a farm yia-yia-yo old mcdonald had a farm yia-yia-yo old mcdonald had a farm yia-yia-yo '''
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.