Python String (str) Type: Basics, Quotes, and Multi-line Strings

In Python, the string (str) is one of the most frequently used data types. It is used to handle text data such as greetings like “Hello”, user IDs, filenames, or error messages. A string is treated as a “sequence of characters.”

This article explains the three basic ways to create strings in Python and how to use them effectively.


目次

1. Single Quotes (')

The most basic way to create a string is to enclose the text in single quotes (').

# Create with single quotes
file_name = 'report.pdf'
status_code = 'OK'

print(file_name)

Output:

report.pdf

2. Double Quotes (")

You can create strings by enclosing them in double quotes (") just as you would with single quotes.

# Create with double quotes
app_title = "Data Management Tool"
user_id = "user-001"

print(app_title)

Output:

Data Management Tool

Choosing Between Single and Double Quotes

Python provides two types of quotes to make it easier to include quote marks inside a string.

  • If you want to include " inside the string, wrap the whole string in '.
  • If you want to include ' inside the string, wrap the whole string in ".
# 1. String containing double quotes "
message_1 = 'He said, "Hello!"'
print(message_1)

# 2. String containing single quotes '
message_2 = "It's a beautiful day."
print(message_2)

Output:

He said, "Hello!"
It's a beautiful day.

3. Triple Quotes (""" or ''')

By enclosing text in three consecutive quotes (""" or '''), you can create a string that spans multiple lines. Both """ and ''' function the same way.

# Create a multi-line string with triple double quotes
sql_query = """
SELECT
    id,
    name,
    email
FROM
    users
WHERE
    is_active = True;
"""

print(sql_query)

Output:

SELECT
    id,
    name,
    email
FROM
    users
WHERE
    is_active = True;

A key feature is that line breaks and indentation are preserved exactly as they are written.

Use Case: Docstrings

Triple quotes are also the standard method for writing function documentation (Docstrings).

def calculate_average(numbers):
    """
    Receives a list of numbers and returns the average.
    
    Args:
        numbers (list): A list of numbers.
        
    Returns:
        float: The calculated average. Returns 0.0 if the list is empty.
    """
    if not numbers:
        return 0.0
    return sum(numbers) / len(numbers)

# View the function documentation
help(calculate_average)

Summary

The Python string (str) is the type used for handling text data.

  • 'Text' (Single Quotes): Basic string creation.
  • "Text" (Double Quotes): Basic string creation. Useful when the string contains '.
  • """Text""" (Triple Quotes): Used for multi-line strings or function documentation (Docstrings).

While all three methods ultimately create the same str type object, using them appropriately improves code readability.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次