Python
strings
Written by:
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.
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 beforeand
, 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.
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.
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
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
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.
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
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.
If you want to learn more about how to concatenate strings in Python and explore the different methods available, we invite you to check out this interactive tutorial.
👉 Explore this tutorial on string concatenation in Python!
In this tutorial, you will learn:
Don’t miss it! This is an essential resource to become a more effective Python developer.