My python journey: 3 ways to concatenate strings

When I began programming, the first thing I did was print Hello World!

print("Hello World!")

Then I learned about storing strings to variables

a = "hello"
b = "world!"

But I didn't know how to combine the 2 words. Until I learned the + operator.

+ operator

print(a+b)
# Output: helloworld!

But it didn't output with space in the middle. I felt frustrated. So I added a space string.

print(a + " " + b)
# Output: hello world!

Satisfied? Definitely! As I gained experience though, I found myself writing ugly code. Therefore I sought more elegant ways of writing code. I experimented with str.format()

str.format()

print("{} {}".format(a, b))

Then recently, I have been using f'string. I prefer this as it is a cleaner way to format strings.

f"string"

print(f"{a} {b}")

All of these approaches are valid and have their place. Remember it is all about context and applying the best solution.

Until then -> Happy coding!