Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to append to a string in Python

Getting Started

Python, a high-level language, is known for its simplicity and readability. One of the fundamental aspects of any programming language is manipulating and interacting with text data. In Python, this data type is known as a string. In this post, we will delve into one of the fundamental operations of strings - appending.

Appending is a fancy term for 'adding to the end'. Think of it as adding a new car to the end of a train. The train can still move, it just has an extra car. In Python, appending to a string means adding more text to the end of the existing string.

The Basic Way: Using the '+' Operator

The simplest way to append to a string in Python is by using the '+' operator. This operator, when used with strings, concatenates (joins) two strings.

str1 = "Hello"
str2 = "World"
str1 = str1 + " " + str2
print(str1)

In the above code, we first define two strings str1 and str2. The str1 string is then appended with a space " " and str2. The final result is Hello World.

Augmented Assignment: Using '+=' Operator

Augmented assignment is a fancy term for a shortcut way of writing code. In Python, the '+=' operator is an example of an augmented assignment operator. This operator can also be used to append to a string.

str1 = "Hello"
str1 += " World"
print(str1)

In the above code, we first define a string str1. The str1 string is then appended with " World" using '+=' operator. The final result is Hello World.

Join Method: Appending Multiple Strings

What if we have more than two strings? Or a list of strings that we want to append? The join() method can be our savior in such situations.

str_list = ["Hello", "beautiful", "world"]
result = " ".join(str_list)
print(result)

In this code, we have a list of strings named str_list. The join() method is then used to append all the strings in the list, separated by a space. The final result is Hello beautiful world.

Format Method: Appending with Style

Python provides us with a way to append and format our strings using the format() method. This method allows us to insert and format our strings in a cleaner and more readable way.

name = "John"
greeting = "Hello, {}!".format(name)
print(greeting)

In this code, we define a string name. The format() method is then used to insert the value of name into the string. The final result is Hello, John!.

F-Strings: The Modern Way of Appending

F-Strings, introduced in Python 3.6, offers a more readable, concise, and less prone to error way of appending and formatting strings.

name = "John"
greeting = f"Hello, {name}!"
print(greeting)

In this code, we define a string name. The f-string is then used to insert the value of name into the string. The final result is Hello, John!.

Conclusion

Strings are like a train of characters and appending is like adding more cars to the train. Python makes it easy to append strings using a variety of methods such as '+' operator, '+=' operator, join() method, format() method and the modern f-string. These methods not only make your code more readable but also allow you to handle different scenarios of string manipulation. Just like a skilled conductor knows when and how to add cars to his train, a proficient Python programmer knows when and how to use these methods to append to his strings. So, hop on the Python train and enjoy the ride!