Python string append | How to Append String in Python

featured Image

In this article, we will learn to append multiple strings or characters in a python program.

In Python, strings are immutable objects, which means we cannot modify the original string. However, we can create new strings from an existing one in python.

There are various ways to modify and add two or more strings together like using the f-string, join function or the += operator.

Ways to append strings in Python

In Python there are three common ways to append strings :

  1. Using the += operator
  2. Using join() function
  3. Using f-string

Let’s see each of the methods with an example.

Concatenate strings using += operator

We can concatenate two strings into a single string using the += operator in python.

Using this operator we can append one string upto another to create the final string.

Example:

str1 = 'Hello '
str2 = 'How are you?'

str1+=str2

print(str1)

Output:

Hello How are you?

Here, we have defined two string variables (str1 and str2) that we want to append to one another.

Then we used the += operator to append the second string with the first one and print it out in the terminal.

Using += operator we can merge only two strings together. So if want to concatenate multiple strings then we have to use the for loop statement with += operator.

Let’s see an example to append string in python using for loop.

str_list = ['hello ', 'my name is', ' Jeff']
append_str = ""
for item in str_list:
    append_str += item

print(append_str)  

Output:

hello my name is Jeff

In the example, we have a list of string. We then loop through each string in the list using the for loop statement.

On each loop, we append each individual string with the other using the += operator.

Append string using String.join() function

To append strings together in python we can use the join() function. It takes in a list or tuple of strings and then using the join() function it merges all the strings to create a new final string.

The join() method takes in iterables and then merges them into one string.

Example:

str_list = ["hello","This is me.","How are you?"]

append_str = ' '.join(str_list)

print(append_str)

Output:

hello This is me. How are you?

The empty string (' ') is the character that acts as a separator between the strings. In the example, we have used a whitespace as a separator.

You can append different characters to strings using the join() function.

Append string using f-string

In version 3.6, python introduced the new f-string that is used to format strings in python. It is called the formatted string literals.

It is the easiest way to format strings in python. It is more readable, faster and less prone to errors.

To format a string using f-string literals we have to use f at the beginning of the string and then the curly braces containing the expressions to be replaced by their values.

Now, using this f-string we will append one string with another easily in the example below.

Example:

str1 = 'Hello'
str2 = 'My name is Jeff.'

append_string = f'{str1} {str2}'
print(append_string)

Output:

Hello My name is Jeff.

Using f-string we can join multiple strings easily into one single string in python.

Conclusion:

In this post, we have learned and seen different methods to append strings in a python program.


Related Topics:

Python – Remove Newline From Strings (3 Ways)

Python – Check if a string is Empty or Not

Replace character in String by index in Python

Split string by whitespace in python

Convert a string into integer or float using Python

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *