Skip to content

Literal String Concatanation

The core concept you are looking at here is a special feature in Python called Implicit String Literal Concatenation.

Let's break down exactly what is happening, why it throws errors in certain cases, and how this behaves differently in a .py file compared to the REPL (the interactive terminal).

1. The Core Concept: Implicit String Concatenation

In Python, if you place two or more string literals next to each other (separated only by whitespace or parentheses), Python's parser will automatically glue them together into a single string before the code even runs.

A string literal is raw text explicitly written in quotes right in your code.

# Python sees this:
text = ('Put several strings within parentheses '
        'to have them joined together.')

# And immediately translates it to this behind the scenes:
text = 'Put several strings within parentheses to have them joined together.'

This feature exists purely to make formatting long text easier for developers so they don't have to use the + operator multiple times across multiple lines.

2. Why do the errors happen?

This implicit joining only works with literals. It does not work with variables or expressions. Here is why:

  • Variables: When you write prefix 'thon', Python sees a variable name (prefix) sitting right next to a string literal ('thon') with no operator (like +) between them. Because prefix is evaluated at runtime (when the program is actually running), the parser gets confused during the reading phase and throws a SyntaxError.
  • Expressions: Similarly, ('un' * 3) is a mathematical expression that must be calculated. The parser cannot implicitly join a calculation with a literal string ('ium'), so it throws a SyntaxError.

To join a variable or a calculation with a string, you must use the + operator, because + tells Python to do the joining dynamically at runtime.

3. What happens if you put this in a .py file?

This is a great question. There is a major difference between how the REPL (the interactive terminal) and a standard .py script output data.

In the REPL: When you type a variable name or an expression and press Enter, the REPL automatically prints its value to the screen.

>>> text = 'hello' 'world'
>>> text
'helloworld'  # The REPL prints this automatically

In a .py script: If you put that exact code into a file (e.g., script.py) and run it, nothing will print to your screen. In a script, Python evaluates the expressions, but it keeps the results quietly in memory unless you explicitly tell it to show them using the print() function.

Here is how your exact REPL code must be adapted to work in a .py file:

# 1. This works, but won't print anything to the console on its own
text = ('Put several strings within parentheses '
        'to have them joined together.')

# You MUST use print() in a .py file to see the output
print(text) 

# 2. This will still crash the .py file before it even runs (SyntaxError)
# prefix = 'Py'
# print(prefix 'thon') 

# 3. This is the correct way to do it in a .py file
prefix = 'Py'
print(prefix + 'thon') 

Summary: Implicit concatenation is just a neat syntax trick for hard-coded strings. If variables or math are involved, always use + (or f-strings!). And remember: the REPL auto-prints everything, but .py files require print().