How do you concatenate text in python?

❮ Python Glossary

String Concatenation

String concatenation means add strings together.

Use the + character to add a variable to another variable:

Example

x = "Python is "
y = "awesome"
z =  x + y
print[z]

Try it Yourself »

Example

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print[c]

Try it Yourself »

Example

To add a space between them, add a " ":

a = "Hello"
b = "World"
c = a + " " + b
print[c]

Try it Yourself »

For numbers, the + character works as a mathematical operator:

Example

x = 5
y = 10
print[x + y]

Try it Yourself »

If you try to combine a string and a number, Python will give you an error:

Example

x = 5
y = "John"
print[x + y]

Try it Yourself »

Related Pages

Python Variables Tutorial Creating Variables Variable Names Assign Value to Multiple Variables Output Variables Global Variables

❮ Python Glossary


Educative Answers Team

In Python, you can concatenate two different strings together and also the same string to itself multiple times using + and the * operator respectively.

Use the + operator

The + operator can be used to concatenate two different strings.

The following illustration shows how it is done:

str1="Hello"
str2="World"
print ["String 1:",str1]
print ["String 2:",str2]
str=str1+str2
print["Concatenated two different strings:",str]

Use the * operator

Appending the same string to a string can be done using the * operator, as follows:

str=str * x
  • Where x is the number of times string str is appended to itself.

The following illustration shows how it is done:

str1="Hello"
print ["String 1:",str1]
str1=str1*3
print["Concatenated same string:",str1]

Note: When concatenating a variable of non-string type with a string, you first need to cast that variable into a string.

RELATED TAGS

python

concatenation

append

prepend

join

Copyright ©2022 Educative, Inc. All rights reserved

Python string concatenation is the process of merging two or more strings. The + operator adds a string to another string. % lets you insert a string into another string value. Both operators are used to concatenate strings in Python.

When you’re working with a string in Python, you may want to combine it with another string. For example, you may have a user’s first name and surname and want to combine them together to get the user’s full name.

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

We call merging strings together string concatenation. Python offers a number of string methods that can be used to concatenate separate strings and return a new one.

In this tutorial, we are going to discuss how to use the + and % operators to concatenate strings in Python.

How to Concatenate Strings in Python

You can concatenate strings in Python using the + and % operators. The + operator adds a value to the end of a string whereas the % operator adds a value to any position in a Python string.

Python Concatenate Strings Using +

The + operator lets you combine two or more strings in Python. This operator is referred to as the Python string concatenation operator. The + operator should appear between the two strings you want to merge.

The syntax for the + operator is:

print["Hello " + "World"]

This code concatenates, or merges, the Python strings “Hello ” and “World”. The Python print[] statement displays the final string to the console.

Notice that we added a space after the word “Hello “. This is because concatenation does not automatically add a space between the strings we merge.

Optionally, we could assign our strings to string variables. This is useful if you are working with multiple strings that you want to merge.

The + operator returns a new string. This is because strings are immutable. which means they cannot be changed. Any string method in Python returns a new string, no matter what format method you use. The string that is created can be assigned to a new variable.

Concatenate Strings Python with + Example

Let’s say that you are working for the payroll department at a company. You want to print out an employee’s contact information to the console. Instead of just printing out the name of the employee and their address, you want to return the information with labels.

You could use string concatenation to merge labels with the employee’s contact information to get your desired output. Here’s an example of a program that merges this information:

print["Name: " + "John"]
print["Address: " + "San Francisco"]

Our program returns the following output:

Name: John
Address: San Francisco

As you can see, our two strings on each line have been concatenated with the operator +. On our first line, the string Name: John was created, and on the second line, the string Address: San Francisco was created.

In the above example, we added white space to the end of our first strings [“Name: “ and “Address: “]. This is not done by default, as we discussed earlier.

Converting a Value to an Integer

The + string operator can only be used to merge two string values. You cannot combine data of two different data types using concatenation.

So, if you have an integer and a string that you want to combine, you’ll need to convert the integer to a string. Here’s what happens if we try to combine a string and an integer:

Our code returns the following error:

TypeError: can only concatenate str [not "int"] to str

You can learn more about how this error works and why it is raised in our “Python typeerror: can only concatenate str [not “int”] to str Solution” article.

We can fix this error by using the str[] method to convert the integer value to a string. Our code will return the intended output:

Our code returns: John22.

Concatenate Strings Python Using “%”

The % string formatting operator merges a string into another string. This operation is commonly called Python string interpolation. You can add a string to any position in an existing string using interpolation.

Whereas the + operator adds a value to the end of a string, the % operator can add a value to at position that you specify.

Let’s look at the syntax for the % operator:

print["%s %s" % ["Hello", "World"]]

This code creates a single string with the contents “Hello World”. The %s values represent a value from the tuple that appears after our string. We specify the values that should appear in the string after the % which follows our string.

We did not use specify a space in any of the values to add to our string. This is because we separated each %s value with a space in our main string.

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Let’s use an example to illustrate how the % operator works.

% Operator Example

Say that we have two names that we want to appear in a string. Here’s the code we could use to add those names to our string:

jack_and_jill = "%a and %b went up the hill." % ["Jack", "Jill"]
print[jack_and_jill]

Our code returns:

Jack and Jill went up the hill.

We use the % operator to declare where we want a value to appear in our string.

We use %a to say that we want a specific value to appear in that position. %b tells our program that we want another value to appear in that position. Then, at the end of our string, we use the % operator and tell our code what values we want to appear in our list.

We have told our code that we want Jack to be placed in the first open place in our code. Jill should appear in the second open place. That leaves us with the string Jack and Jill went up the hill.

Conclusion

String concatenation is a common operation in many programming languages where you combine two or more strings into one. For example, you may want to combine an employee’s first name and surname into one string, or two different flavors of pizza into one.

In this tutorial, we discussed how you can use the + operator to concatenate strings in Python. We also explored how you can use the % operator to interpolate strings in Python.

To learn more about Python, read our complete How to Learn Python guide.

How do you concatenate values in Python?

Use the + operator.
str1="Hello".
str2="World".
print ["String 1:",str1].
print ["String 2:",str2].
str=str1+str2..
print["Concatenated two different strings:",str].

How do you concatenate 3 strings in Python?

To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.

What is the best way to concatenate strings in Python?

One of the most popular methods to concatenate two strings in Python [or more] is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.

How do you concatenate strings in a list in Python?

You can concatenate a list of strings into a single string with the string method, join[] . Call the join[] method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

Chủ Đề