Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
← Back to How to's
Edit on Github

How to concatenate Strings on Python

Written by:

How to concatenate Strings on Python

String concatenation is the process of adding two strings together in one. The easiest way to concatenate strings in Python would be to use the format string method. This method consists on adding the letter "f" before the string quote marks like this f"Hello world" , then you can concatenate variables by calling them between brackets {} inside the string quote marks. Here is an example of how to use it:

1name = "4Geeks" 2 3print(f"Hello from {name}!") 4#Output -> Hello from 4Geeks!

In the previous example, we are using the variable name to concatenate it to the string.

String concatenation is a common action we, as software developers, have to deal with in almost any project. There are many ways you can concatenate strings, you can use any of them so pick the one that suits you the most.

Concatenate strings on Python using the + operator

The most familiar way to concatenate strings for most people is the + operator. Check this example that explains how it works:

1name = "Alex" 2 3print("Hello my name is " + name + " and I'm a developer") 4#Output-> Hello my name is Alex and I'm a developer

Notice the space after is and before and , if we didn't added them, then the output would have looked like this: Hello my name isAlexand I'm a developer. Remember to add the spaces when needed.

Concatenate strings using the += operator

We can use the += operator to concatenate strings as well, it's pretty similar to using the + operator, but in this we will modify the value of the original string.

1#Concatenate using the += operator 2fullname = "Alex " 3lastname = "Smith" 4fullname += lastname 5print(fullname) 6#Output-> Alex Smith

Notice that there is a space inside the string, otherwise fullname's value would be "AlexSmith". Remember to add the spaces when needed.

Concatenate an int or float value with text using str()

Numbers and strings are data type completely different. When we want to add a number (int, float, etc) to a string, we will need to use the str() to convert that number into a string.

1# Concatenate using the + Operator with numbers 2geeks_word = "Geeks" 3geeks_number = 4 4print("At " + str(geeks_number) + geeks_word + " Academy we love Python! <3") 5#Output-> At 4Geeks Academy we love python! <3

As stated above, we are adding spaces for the render to respect writing rules.

If we would not convert the int1 number variable to a string, we would receive the following error letting you know you are trying to concatenate an Integer with a String.

1TypeError: can only concatenate str (not "int") to str

Concatenate strings on Python using the f-Strings

Python f-Strings (formatted string literals) makes writing strings faster and much easier. This option was introduced on Python 3.6 so keep in mind backward compatibility when using it.

This is one of the most used methods among experienced developers because of the ease to insert variables into the curly brackets. The variable(s) will be evaluated and a string representation will be displayed.

Implementation:

1#Concatenate using the f-Strings method 2geek_word = "Geeks" 3print(f"Showing the geek_word variable: {geek_word} using f-String") 4#Output-> Showing the geek_word variable: Geeks using f-String

Making use of the f-Strings concatenation, we don't need to convert numbers (Int, Float, etc) to string to be able to display them inside the curly brackets.

1#Concatenate using the f-Strings method with numbers 2geek_word = "Geeks" 3geek_number = 4 4print(f"At {geek_number}{geek_word} Academy we love Python! <3") 5#Output-> At 4Geeks Academy we love python! <3

Concatenate using .format()

Another way to concatenate strings in Python is by making use of the format() method. It works similar to f-Strings since they both use curly brackets {} to insert variables but is available from version 2.7.

This method doesn't need either to convert the number values into strings.

1#Concatenate using the .format() method 2geek_word = "Geeks" 3geek_number = 4 4text = "{}{} Academy".format(geek_number, geek_word) 5print(text) 6#Output-> 4GeeksAcademy

As you can see, the result returned is the expected, but without spacing between words.

Concatenate using % operator

The % operator is an older way to concatenate strings that work very similar to the .format() method. We use %s as placeholders for different values, as we did with the curly brackets in previous examples.

1#Concatenate using the % Operator 2geek_word = "Geeks" 3geek_number = 4 4text = "%s%s Academy" % (geek_number, geek_word) 5print(text) 6#Output-> 4Geeks Academy

Concatenate using the * Operator

The use of the * Operator is not a concatenation in the way we have been doing so far. The * Operator is used for multiplication and that's exactly what it does, multiplying for the amount given the string.

1#Concatenate using the * Operator 2str1 = "4Geeks Academy " 3print(str1*3) 4#Output-> 4Geeks Academy 4Geeks Academy 4Geeks Academy

If you format the string variable correctly (leaving a space at the end of the sentence/word), this is the best way to repeat a line of text as many times as you may need.

You can read more related topics at 4Geeks. Hope you enjoyed the reading and keep on the Geek side!