---
title: 4.12 Multiline Strings
description: "A **multiline string** is a string that spans across multiple lines. Python allows multiline strings to be created using **triple single quotes (`'''`)** or **triple double quotes (`\"\"\"`)**. Unlike normal strings, multiline strings preserve line breaks, spaces, and indentation exactly…"
url: "https://edcret.in/docs/python-programming/strings-python/4-12-multiline-strings/"
updated: "2026-07-29"
category: Python Programming / Strings
---

# 4.12 Multiline Strings

A **multiline string** is a string that spans across multiple lines.

Python allows multiline strings to be created using **triple single quotes (`'''`)** or **triple double quotes (`"""`)**.

Unlike normal strings, multiline strings preserve line breaks, spaces, and indentation exactly as they are written.

Multiline strings are commonly used for long text, documentation, SQL queries, HTML templates, and formatted messages.

---

## Syntax

```
"""
Multiple
lines
of text
"""
```

or

```
'''
Multiple
lines
of text
'''
```

### Components

| Component | Description |
| --- | --- |
| `"""` or `'''` | Triple quotes used to begin and end a multiline string. |
| Text | The content of the multiline string. |

---

## Example 1: Creating a Multiline String

**main.py**

```
message = """Hello
Welcome to Python
Programming"""

print(message)
```

### Output

```
Hello
Welcome to Python
Programming
```

### Explanation

- The string is enclosed within triple double quotes.
- Each new line becomes part of the string.
- `print()` displays the text exactly as stored.

---

## Example 2: Using Triple Single Quotes

**main.py**

```
text = '''Python
is
easy to learn.'''

print(text)
```

### Output

```
Python
is
easy to learn.
```

### Explanation

- Triple single quotes also create multiline strings.
- They behave the same as triple double quotes.
- You can choose either style.

---

## Example 3: Preserving Blank Lines

**main.py**

```
text = """Line 1

Line 3"""

print(text)
```

### Output

```
Line 1

Line 3
```

### Explanation

- Blank lines are preserved.
- The empty line becomes part of the string.
- Python stores the string exactly as written.

---

## Example 4: Including Both Single and Double Quotes

**main.py**

```
text = """She said, "Python is fun."
It's easy to learn."""

print(text)
```

### Output

```
She said, "Python is fun."
It's easy to learn.
```

### Explanation

- Triple quotes allow both single and double quotation marks inside the string.
- No escape characters are required unless three consecutive quote characters appear.

---

## Example 5: Multiline String with Indentation

**main.py**

```
text = """
Python
    Programming
        Tutorial
"""

print(text)
```

### Output

```
Python
    Programming
        Tutorial
```

### Explanation

- Spaces and indentation are preserved.
- The leading blank line is also part of the string because the opening triple quotes are followed by a newline.

---

## Example 6: Assigning to a Variable

**main.py**

```
paragraph = """Python is a high-level language.
It is easy to learn.
It supports multiple programming paradigms."""

print(paragraph)
```

### Output

```
Python is a high-level language.
It is easy to learn.
It supports multiple programming paradigms.
```

### Explanation

- The multiline string is stored in the variable `paragraph`.
- Each line remains separate.
- The string is printed exactly as stored.

---

## Example 7: User Input with Multiline Output

**main.py**

```
name = input("Enter your name: ")

message = f"""Welcome,
{name}
to Python."""

print(message)
```

### Sample Input

```
Alice
```

### Output

```
Welcome,
Alice
to Python.
```

### Explanation

- A multiline string can contain expressions (using an f-string).
- Each line remains part of the string.
- The user's input is inserted into the appropriate position.

---

## Example 8: Counting Characters

**main.py**

```
text = """ABC
DEF"""

print(len(text))
```

### Output

```
7
```

### Explanation

- The newline character between `ABC` and `DEF` is also counted.
- The string contains:
  - `ABC` → 3 characters
  - newline (`\n`) → 1 character
  - `DEF` → 3 characters
- Total length = `7`.

---

## Example 9: Checking the Data Type

**main.py**

```
text = """Python
Programming"""

print(type(text))
```

### Output

```
<class 'str'>
```

### Explanation

- A multiline string is still a normal string.
- Its data type is `str`.

---

## Example 10: Using a Multiline String as Documentation

**main.py**

```
description = """
This program
demonstrates
multiline strings.
"""

print(description)
```

### Output

```
This program
demonstrates
multiline strings.
```

### Explanation

- Multiline strings are often used to store long descriptions or documentation.
- The formatting is preserved exactly as written.

---

# Common Mistakes

### 1. Using Single Quotes for Multiple Lines

**Incorrect**

```
text = "Hello
Python"
```

### Output

```
SyntaxError: unterminated string literal
```

**Reason**

Normal strings cannot span multiple lines.

**Correct**

```
text = """Hello
Python"""
```

---

### 2. Assuming Indentation Is Ignored

**Incorrect**

```
text = """
    Python
"""

print(text)
```

### Incorrect Expectation

```
Python
```

### Actual Output

```
    Python
```

**Reason**

Spaces inside a multiline string are preserved.

---

### 3. Forgetting That Blank Lines Are Included

**Incorrect**

```
text = """

Python
"""

print(text)
```

### Reason

The blank line after the opening triple quotes becomes part of the string.

---

### 4. Assuming Triple Quotes Create a Different Data Type

**Incorrect**

```
text = """Python"""

print(type(text))
```

### Incorrect Expectation

```
<class 'multiline_string'>
```

### Actual Output

```
<class 'str'>
```

**Reason**

A multiline string is still a normal string of type `str`.

---

# Best Practices

- Use triple quotes for long blocks of text.
- Choose either triple single quotes or triple double quotes and use them consistently.
- Be mindful that indentation and blank lines become part of the string.
- Use multiline strings for documentation, templates, and formatted messages.
- Use f-strings with triple quotes when inserting variables into multiline text.

---

# Key Points to Remember

- Multiline strings are enclosed in triple quotes.
- Both `'''` and `"""` can be used.
- Line breaks are preserved.
- Spaces and indentation become part of the string.
- Multiline strings are of type `str`.
- Blank lines are preserved.
- Multiline strings are useful for long text, documentation, and formatted output.
