Python online course

Concatenating Strings in Python

Learn how to concatenate Python strings with +, add spaces and punctuation, fix str and int TypeErrors, and use str() to build readable text.

String concatenation means joining separate text values end to end to create one new string. A string is a sequence of text characters, commonly written inside quotation marks, such as 'Hello' or "Python".

Python creates a new combined string value when it concatenates strings. The original string values are not changed.

This lesson assumes you know how to create variables, write basic strings, use integer values, and call print(). The print() function displays a value in the program output.

Using the Plus Operator With Strings

The plus operator, written as +, joins string operands when the operands are strings. An operand is a value used by an operator. You can combine string variables, string literals, or both.

A variable is a named reference that stores a value. This example stores two parts of a name, combines them, and assigns the result to a new variable:

name = 'Mike'
surname = 'Tyson'
full_name = name + surname
print(full_name)

Output:

MikeTyson

The expression name + surname produces a new string, and the variable full_name refers to that combined value.

Adding Spaces and Punctuation

Python does not insert a space automatically when strings are joined. In the previous example, 'Mike' and 'Tyson' become 'MikeTyson' because no separator was included.

A string literal is text written directly in source code. A space can be written as a string literal containing one space:

name = 'Mike'
surname = 'Tyson'
full_name = name + ' ' + surname
print(full_name)

Output:

Mike Tyson

The middle part, ' ', is a real string containing one space. Spaces and punctuation must be included deliberately in the string literals that surround your values.

For example, these fragments create a readable sentence:

first_name = 'Ada'
language = 'Python'
sentence = first_name + ' is learning ' + language + '.'
print(sentence)

Output:

Ada is learning Python.

The space after learning and the period in the final literal are both part of the text. If either is missing, the output will not have the intended formatting.

Strings and Data Types

A data type is the category of a value. For this topic, the important types are str, which represents text, and int, which represents an integer. An integer is a whole-number value such as 30 or -4.

The + operator can concatenate strings only when every operand being joined is a string. It cannot directly join a string and an integer:

age = 30
print('You are ' + age + ' years old.')

This raises a TypeError. A TypeError is an error raised when an operation receives incompatible value types. Here, the text fragment is a str, but age is an int. Python does not automatically turn the integer into text for string concatenation.

Converting Values With str()

str() is a built-in function that creates a string representation of a value. Convert the integer before concatenating it with sentence text:

age = 30
print('You are ' + str(age) + ' years old.')

Output:

You are 30 years old.

The call str(age) creates the text value '30'. The original variable age still represents the number 30, so it remains suitable for numeric operations. The conversion changes the value used in this particular text-building expression; it does not change the stored meaning of age.

How to Read a Concatenation Expression

Consider this complete expression:

name = 'Mina'
age = 30
message = 'Hello, ' + name + '. You are ' + str(age) + ' years old.'
print(message)

Read it from its separate pieces:

  1. 'Hello, ' is a string literal containing greeting text and a trailing space.
  2. name is a variable containing the string 'Mina'.
  3. '. You are ' is another literal containing punctuation and spaces.
  4. str(age) converts the integer in age into the string '30'.
  5. ' years old.' is the final literal, including a leading space and a period.
  6. Python joins these pieces from left to right, producing one final string.
  7. print(message) displays that final string.

The output is:

Hello, Mina. You are 30 years old.

When reading a longer expression, identify each variable, literal text fragment, separator, and converted value. Then check that every value being joined is a string and that spaces and punctuation appear in the correct fragments.

Troubleshooting Concatenation

Words run together

If the output is MikeTyson instead of Mike Tyson, no separator was included. Add a space string literal:

full_name = name + ' ' + surname

You can also put required spacing inside an adjacent text literal, as in 'Hello, '.

A TypeError appears when joining text and a number

If a sentence fragment is joined directly with an integer, the str and int types are incompatible for this operation. Convert the number at the point where it becomes part of the text:

message = 'Age: ' + str(age)

Sentence spacing is incorrect

Inspect every literal around each variable or converted value. Add a space before a value when the preceding text needs one, or after a value when more words follow:

message = 'You are ' + str(age) + ' years old.'

Here, the first literal ends with a space, and the last literal begins with a space. Removing either space would join words together.

Key Points

  • Concatenation joins strings end to end and produces a new combined string.
  • Use + to concatenate string variables and string literals.
  • Python does not add spaces or punctuation automatically.
  • Every operand in a + concatenation must be a string.
  • Use str() to convert an integer or another value into text for sentence construction.
  • Converting a value for an expression does not turn the original numeric variable into text.
  • For larger formatting tasks, continue with topics such as string functions and other Python string-formatting techniques.