Append To String Python Examples

In Python, the string is an object that can’t be changed.

By adding two strings together with the “+” operator, you can make a new string.

There are many ways to do this, such as using join, format, string IO, and appending the strings with space.

Python Append String

Use the += operator in Python to append a string to another string.

The += operator in Python joins two strings together. It adds up two numbers and gives the total to a variable.

There are other ways you can append to a string.

  1. Using string.join() method
  2. Using Python f-strings

Append String Using += Operator In Python

When you use the += (plus equal operator) to concatenate two strings together, a new string is made, but the original string stays the same.

You can use the (+=) operator to do the append task.

Example:

fname = "AngelJude"
lname = "Suarez"

# printing fname string
print("The first name: " + str(fname))

# printing lname add string
print("The last name : " + str(lname))

# Using += operator adding one string to another
fname += lname

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

When you execute the program, this will be the output:

The first name: AngelJude
The last name : Suarez
The concatenated string is: AngelJudeSuarez

Explanation:

First, we defined two strings, and we will append the second string named “Suarez” to the first string named “AngelJude.“

Then, we printed the two strings, and then we used the += operator to concatenate or append two strings.

Append String Multiple Times In Python

By making a separate function, you can append strings more than once.

Let’s make a user-defined function that adds the string to the original string n times.

Example:

str = 'Jude'


def string_append(s, n):
    op = ''
    i = 0
    while i < n:
        op += s + '-'
        i = i + 1
    return op


pstring = string_append(str, 5)
print(pstring)

When you execute the program, this will be the output:

Jude-Jude-Jude-Jude-Jude-

Explanation

In this example, we made a string and a function that takes str and the number of times as inputs.

Then, we use a while loop to join the strings together until they reach the number of times we set, and it stops when the condition becomes False.

The function string_append() returns multiple strings that have been joined together.

String joint() Method To Append A String

Use the string join() method in Python to append to a string.

To do this, you must make a list and append the strings to it.

Then, use the string join() function to combine them into one long string.

Example:

fname = "AngelJude"
lname = "Suarez"

# printing fname string
print("The first name: " + str(fname))

# printing lname add string
print("The last name : " + str(lname))

# Create a list of Strings
listOfStrings = [fname, lname]

finalString = "".join(listOfStrings)

# print the final result
print("The appended string is: " + finalString)

When you execute the program, this will be the output:

The first name: AngelJude
The last name : Suarez
The appended string is: AngelJudeSuarez

Frequently Asked Questions (FAQs)

How to append to a string in python?

To append a string to another in Python, use the += operator.
The Python += operator appends a string to another.
It adds two values together and assigns the final value to a variable.

Can you append something to a string?

Concatenation of strings refers to joining two or more strings together as if links in a chain.
You can concatenate in any order, such as concatenating str1 between str2 and str3.
Appending strings refers to appending one or more strings to the end of another string.

How do you append a string in a for loop in Python?

To concatenate strings, we will use a for loop, and the “+” operator is the most common way to concatenate strings in Python.

Conclusion

We have completely discussed this article about Append To String Python, which we learned with the help of examples.

I hope this simple Python tutorial will help you a lot.

If you have any questions or suggestions about this simple Python tutorial, please feel free to comment below.

Thank You!

Leave a Comment