String Concatenation – An Interactive Guide

5/5 - (4 votes)

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!

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.

πŸ‘‰ 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 puzzle app: Test your skills now!

Watch the Video About String Concatenation

I found this excellent video on YouTube:

Beginner Python Tutorial 20 - Concatenation

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!

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.

Join the free webinar now!

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'

old mcdonald had a farm
old mcdonald had a farm
old mcdonald had a farm

Leave a Comment