Creating Strings in Python

Learn every way to create a Python string using single quotes, double quotes, triple quotes, the str() constructor, and implicit concatenation.

5 min read

Creating strings in Python is something you will do hundreds of times in every program you write, and Python gives you more ways to create strings than most other programming languages. This flexibility is not just for show; each syntax serves a specific purpose, whether you need to embed quotes inside your text, write paragraphs that span multiple lines, convert a number to text for display, or break a long string across several lines of source code for readability. Understanding all the available options means you can choose the cleanest syntax for each situation instead of fighting against awkward escape sequences.

All of the creation methods described in this article produce the same str type that was introduced in the overview article on Python strings. Once a string exists, it does not remember how it was created. A string made with single quotes, a string made with triple quotes, and a string made with the str() constructor are all identical str objects that support the same operations. The remaining articles in this section then show you how to access individual characters inside these strings and extract substrings with slicing.

Single quotes and double quotes

The two most common ways to create a string are wrapping text in single quotes or double quotes. Python treats both identically, and the resulting object is the same str type in both cases. This is different from languages like Java or C where single quotes create a character type and double quotes create a string. Python has no separate character type, so both quote styles produce full string objects.

The reason Python offers both is practical: it lets you include the other quote character inside your string without needing an escape character. If your text contains a double quote, wrap the whole string in single quotes. If your text contains a single quote or an apostrophe, wrap it in double quotes. For instance, 'She said "Hello" when she arrived' uses single quotes to avoid escaping the inner double quotes, while "It's a beautiful day outside" uses double quotes to avoid escaping the apostrophe. This simple rule eliminates the need for backslash escapes in the majority of everyday strings.

When your string contains both single and double quotes, you have two choices: either escape one type of quote with a backslash, or use triple quotes instead. Writing something like mixed = 'She said "It's time to go"' works for short strings with one or two escaped characters, but for longer text that mixes both quote styles freely, triple quotes are usually cleaner and more readable.

The choice between single and double quotes is largely a matter of style and consistency. Many Python codebases adopt a convention of using double quotes by default, similar to JSON, while others prefer single quotes for their cleaner appearance without the Shift key. Both are correct, and Python's official style guide does not mandate one over the other. The important thing is to pick one style and use it consistently throughout a project.

Triple-quoted strings for multiline text

When you need a string that spans multiple lines, or a string that contains both single and double quote characters without escaping, triple quotes are the right tool. Python supports two forms of triple quotes: three consecutive double quotes or three consecutive single quotes. As with the single-line forms, the two are functionally identical, and you choose between them based on which quote character appears inside your text.

Triple-quoted strings preserve the line breaks you type in your source code. Every press of the Enter key between the opening and closing quotes becomes a newline character in the resulting string. This makes triple quotes ideal for embedding paragraphs of text directly in your code, writing formatted output like ASCII art or email templates, and defining docstrings that document your modules, functions, and classes.

pythonpython
paragraph = """This string spans
three separate lines
in the source code."""
 
print(paragraph)

The output shows that the newline characters are part of the string. If you want to start the text on the line after the opening quotes without including a leading blank line in the string, you can put a backslash immediately after the opening quotes. The backslash tells Python to ignore the newline that follows it, so the text begins on the second line of the source without a blank first line in the resulting string.

Triple-quoted strings are also the standard way to write docstrings in Python. A docstring is a string literal that appears as the first statement in a module, function, class, or method definition, and it describes what that code element does. Documentation tools and the built-in help() function read docstrings automatically, so using triple quotes for this purpose is a Python convention you will see in every well-documented codebase.

The str() constructor for converting values

Not every string starts as a literal in your source code. Much of the time, you need to create a string from a value that is already stored in a variable, such as a number from a calculation, a boolean from a condition check, or an object from a function return. The built-in str() function handles all of these conversions and produces a string representation of whatever you pass to it.

When you call str() with an integer, it returns the decimal digit representation of that number. When you call it with a float, it returns a string with a decimal point and enough digits to distinguish the value. When you call it with True or False, it returns the strings "True" or "False". When you call it on an object that defines a custom string conversion method, Python calls that method and uses its return value.

pythonpython
str(42)          # '42'
str(3.14159)     # '3.14159'
str(True)        # 'True'
str(None)        # 'None'
str([1, 2, 3])   # '[1, 2, 3]'

The str() constructor is essential whenever you need to build a message by combining text with non-string values. Python does not implicitly convert numbers to strings when you use the plus operator, so trying to write "Score: " + 95 produces a TypeError. Use str() to convert the number first, or better yet, use an f-string which handles the conversion for you behind the scenes.

Implicit concatenation of adjacent literals

Python has a unique feature that surprises many beginners: if you place two or more string literals next to each other with nothing but whitespace between them, Python automatically concatenates them into a single string. This only works with literals, not with variables or expressions, and it happens at compile time before your code runs.

The primary use case for implicit concatenation is breaking a long string across multiple lines of source code without introducing newline characters into the string itself. Instead of writing a single very long line, you can split the string into several adjacent literals, each on its own line, and wrap the whole group in parentheses for readability.

pythonpython
long_message = (
    "This is a very long message that "
    "needs to be split across multiple "
    "lines of source code."
)
 
print(long_message)

This produces a single-line result with all three pieces joined together. Notice the trailing space inside each fragment except the last: without those spaces, the words would run together, because implicit concatenation joins the fragments exactly as they are with no separator added between them. This technique is purely a source-formatting convenience. The resulting string is identical to one written on a single line, and Python discards all knowledge of how the literal was split once the code is parsed.

Rune AI

Rune AI

Key Insights

  • Single and double quotes are interchangeable; use the one that avoids escaping the other quote character.
  • Triple quotes create strings that span multiple lines and preserve internal quote characters.
  • The str() constructor converts any Python object into its string representation.
  • Adjacent string literals are implicitly concatenated, which is useful for breaking long strings across lines.
  • Strings created from function calls and expressions must use explicit concatenation with the plus operator.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between single and double quotes in Python strings?

There is no functional difference. Both single quotes and double quotes create the same str type, and they exist so you can include the other quote character inside the string without escaping it. Use single quotes when the string contains double quotes, and double quotes when the string contains single quotes. For strings that contain both types of quotes, use triple quotes or escape characters.

When should I use triple quotes for creating strings?

Use triple quotes when your string spans multiple lines and you want to preserve the line breaks, or when the string contains both single and double quote characters. Triple-quoted strings are also commonly used for docstrings, which are string literals placed at the start of a module, function, class, or method that document what that code element does.

Can I create a string from a number or other data type?

Yes, use the str() constructor. Passing an integer, float, boolean, or any other Python object to str() returns its string representation. For example, str(42) returns the string '42', and str(True) returns 'True'. This is useful when you need to concatenate numbers with text for display or logging purposes.

Conclusion

Creating strings in Python gives you several syntax options that all produce the same str type. Single quotes, double quotes, triple quotes for multiline text, the str() constructor for converting other types, and implicit concatenation of adjacent literals all have their place. The choice depends on your content: which quote characters are inside the text, whether line breaks matter, and whether you are writing a literal or converting a value. Knowing all the options lets you write cleaner, more readable code.