How to append a string in Python?
Tags: python, programming, codenewbie, tutorial
Published: 2021-10-28 04:42:16

---
title: How to append a string in Python?
published: true
date: 2021-10-24 07:41:41 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/how-to-append-a-string-in-python/
---

ItsMyCode |

In this tutorial, we will see different ways to append a string in Python. The most efficient methods are using the  **+= operator** , **join() function**, and  **f-strings**.

We know that strings are immutable in Python, which means we cannot change the string variable’s value once it’s declared and assigned. However, we can always return a new string.

## Append to a String in Python

We will take a look at 3 different ways to append a string in Python.

1. Using **`+=`** operator
2. Using **`string.join()`** method
3. Using Python **`f-strings`**

## **Method 1: Using += operator to append a string**

The **`+=`** (plus equal) operator is similar to any other language that concatenates two strings and returns a new string in Python without changing the original string.

### **Example: Append to a string using += operator**

```python
# Python program to append to a string using += operator

first_name="Bing"
last_name=" Chandler"

# printing first name
print("The first name is: " + str(first_name))

# printing last name
print("The last name is: " + str(last_name))

# Using += operator adding one string to another
first_name += last_name

# print concatenated string
print("The concatenated string is: " + first_name)
```

**Output**

```python
The first name is: Bing
The last name is: Chandler
The concatenated string is: Bing Chandler
```

## **Method 2: Using join() function to append a string**

Another way to append strings in Python is using the **`join()`** method. It has an advantage over the **`+=`** operator as it can append multiple strings or a list of strings. 

### **Example: Append to a string using the join() function**

In this example, we pass a list of strings to the **`join()`** function, which concatenates them together and returns to the output string.

```python
# Python program to append a string using join() function

first_name="Bing"
last_name=" Chandler"

# printing first name
print("The first name is: " + str(first_name))

# printing last name
print("The last name is: " + str(last_name))

# Using join() method to append a string
result= "".join([first_name,last_name])

# print concatenated string
print("The concatenated string is: " + result)
```

**Output**

```python
The first name is: Bing
The last name is: Chandler
The concatenated string is: Bing Chandler
```

## **Method 3: Using f-strings to append a string**

If you use **Python 3.6** and above version, you can leverage **f-strings** in Python to append a string.

[F-strings](https://www.python.org/dev/peps/pep-0498/)  **provide a way to embed expressions inside string literals** , using a minimal syntax. **F-strings** are prefixed with “`f`” which contains expression inside braces. These expressions are replaced with their values.

### **Example: Append to a string using the f-string**

```python
# Python program to append a string using f-string

first_name="Bing"
last_name=" Chandler"

# printing first name
print("The first name is: " + str(first_name))

# printing last name
print("The last name is: " + str(last_name))

# Using f-string to append a string
result= f"{first_name}{last_name}"

# print concatenated string
print("The concatenated string is: " + result)
```

**Output**

```python
The first name is: Bing
The last name is: Chandler
The concatenated string is: Bing Chandler
```

The post [How to append a string in Python?](https://itsmycode.com/how-to-append-a-string-in-python/) appeared first on [ItsMyCode](https://itsmycode.com).