How to add text to a variable in python

How to add text to a variable in python

Strings in Python are immutable objects. You can not modify the original string, but you can always return a new string. There are various ways, such as using the += operator, join() function, f-strings, and appending the strings with space.

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

There are other approaches you can use to append a string in Python.

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

Append string using += operator in Python

If you use +=(plus equal operator) to concatenate two strings, a new string is created, and the original string won’t change. The (+=)operator can be used to perform the append task.

# app.py

fname = "Millie"
mname = "Bobby"

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

# printing mname add string
print("The middle name : " + str(mname))

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

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

Output

The first name: Millie
The middle name : Bobby
The concatenated string is: MillieBobby

First, we defined two strings in which we will append the second string to the first string.

Then we printed the two strings and then used the += operator to concate or append the string. From the final output, you can see the concatenated string.

Append string multiple times in Python

You can append strings multiple times by creating a separate function. Let’s create a user-defined function that will append the string n times to the original string.

# app.py

str = 'Millie'


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


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

Output

Millie-Millie-Millie-Millie-Millie-

In this example, we defined a string and a function, which takes two parameters: str and number of times. Then we use a while loop to concatenate the strings until they reached the defined number of times, and it will stop after the condition becomes False.

The function string_append() function returns the multiple concatenated strings.

String join() method to append a string

To append a string in Python, use the string join() method. For that, you have to create a list and append the strings to the list. Then use the string join() function to merge them to get the final string.

# app.py

fname = "Millie"
mname = "Bobby"

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

# printing mname add string
print("The middle name : " + str(mname))

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

finalString = "".join(listOfStrings)

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

Output

The first name: Millie
The middle name : Bobby
The appended string is: MillieBobby

Python f-strings

As of the 3.6 version, Python f-strings is a comprehensive new way to format strings. Not only are they more readable, but they are also more concise but also less prone to error than other ways of formatting, and also they are faster! You can also concate the strings using f-strings.

# app.py

fname = "Millie"
mname = "Bobby"

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

# printing mname add string
print("The middle name : " + str(mname))

# use f-strings to concat the strings.
finalString = f"{fname}{mname}"

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

Output

The first name: Millie
The middle name : Bobby
The appended string is: MillieBobby

In this example, it looks like we are formatting two strings using f-string, but we are actually concatenating two strings. So that is it for this tutorial.

Python array append

Python tuple append

Python set append

How do you add a string to a variable?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

How do you add to a variable in Python?

The “+” operator is used to add the variables. The print(“The sum of variables “, variable) is used to get the output.

How do you add words to a string in Python?

The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded. Two strings are added using the + operator.