Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add strings in Python

Understanding Strings in Python

Imagine a string as a sequence of characters. In the world of programming, a character could be a number, a letter, a symbol, or even a space. For example, the word "Hello" is a string composed of the characters 'H', 'e', 'l', 'l', 'o'. In Python, we can create a string by enclosing characters in single quotes (' ') or double quotes (" "). Let's create a string in Python:

greeting = "Hello, World!"
print(greeting)

When you run this code, Python will output Hello, World!, which is the string stored in the variable greeting.

Adding Strings Together: Concatenation

In Python, you can add two strings together. This action is called concatenation. Think of it as gluing together pieces of paper. You take one piece (string), then you take another piece (another string), and you stick them end-to-end. For example:

first_string = "Hello, "
second_string = "World!"
combined_string = first_string + second_string
print(combined_string)

In this example, the + operator is used for concatenation. It takes the string first_string and adds it to the string second_string, forming a new string combined_string. The output will be Hello, World!.

Concatenating More Than Two Strings

Sometimes, you might need to combine more than two strings. Python makes this easy. You can add as many strings as you want using the + operator. Look at the following example:

string1 = "I "
string2 = "am "
string3 = "learning "
string4 = "Python."
full_sentence = string1 + string2 + string3 + string4
print(full_sentence)

The output will be I am learning Python.. In this example, four strings are concatenated together to form a full sentence.

Concatenating Strings and Numbers

What if you want to add a string and a number? For instance, you might want to create a message like "I have 2 apples". You can't directly add a string and a number in Python. If you try, Python will give you an error.

age = 5
message = "I am " + age + " years old."
print(message)

If you run this code, Python will give you a TypeError, saying that it can't convert 'int' object to str implicitly.

To resolve this, you can convert the number into a string using the str() function before concatenation.

age = 5
message = "I am " + str(age) + " years old."
print(message)

This code will output I am 5 years old..

Using the .format() Function for String Concatenation

Python provides a built-in function called format() that you can use for string concatenation. This function allows you to insert values into a string placeholder, {}, and formats it nicely. Here's how you can use it:

age = 5
message = "I am {} years old.".format(age)
print(message)

This code will also output I am 5 years old.

The format() function is particularly useful when you have multiple values to insert into a string.

name = "John"
age = 22
message = "Hello, my name is {} and I am {} years old.".format(name, age)
print(message)

This code will output Hello, my name is John and I am 22 years old.

Conclusion

String concatenation in Python is like creating a sentence from words. Just as we add words together to form a sentence in English, in Python, we combine strings to form a larger string. Whether it's using the + operator, the str() function, or the format() function, Python provides several methods to add strings together.

As you continue your journey in learning Python, remember that practice is key. Try out these examples, play around with them, and come up with your own. The more you practice, the more these concepts will become second nature to you. Happy coding!