This guide shows you how to use string concatenation operators using multiple forms of education:
Ready to learn string concatenation? Let’s get started!
A Textual Introduction to String Concatenation in Python
There are two basic string manipulation operators.
'+'operator concatenates two strings.
'*'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
As a Python professional, you will find yourself using these operators on a daily basis.
👉 Note: Both operands of the string concatenation operation
x + y need to be strings. If you try to append a Boolean to a String, for example, first convert the Boolean to a string.
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
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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.