# String basics in Python

Although strings in Python appear to be simple and straightforward, there's some complexity in string rules that's important to grasp. Knowing the rules helps you avoid being surprised by string behavior when you're modifying values or formatting text.

## A simple string&#x20;

In our example for this unit, you have a single fact about the Moon that's assigned to a variable, like this:

```python
fact = "The Moon has no atmosphere."
print(fact)
```

The output shows that the text has been assigned to the variable: `The Moon has no atmosphere.`

## Immutability of strings

In Python, strings are immutable. That is, they can't change. This property of the string type can be surprising, because Python doesn't give you errors when you alter strings.

You need to add another fact (sentence) to the single fact that's assigned to a variable. It seems as though adding the second fact would alter the variable, as in the following example:

```python
fact = "The Moon has no atmosphere. "
fact + "No sound can be heard on the Moon."
```

You might expect the output to be: `The Moon has no atmosphere. No sound can be heard on the Moon.`&#x20;

Although it might look as though we've modified the variable `fact`, a quick check of the value reveals that the original value is unchanged:

```python
fact = "The Moon has no atmosphere. "
fact + "No sound can be heard on the Moon."
print (fact)
```

Output: `The Moon has no atmosphere.`&#x20;

The trick s that you must use a return value. When you add strings, Python doesn't modify any string, but it returns a new string as the result. To keep this new result, assign it to a new variable:

```python
fact = "The Moon has no atmosphere. "
two_facts = fact + "No sound can be heard on the Moon."
print(two_facts)
```

Output: `The Moon has no atmosphere. No sound can be heard on the Moon.`&#x20;

Operations on strings always produce new strings as a result.

## About using quotation marks

You can enclose Python strings in single, double, or triple quotations marks. Although can use them interchangeably, it's best to use one type consistently within a project. For example, the following string uses double quotation marks:

```python
moon_radius = "The Moon has a radius of 1,000 miles."
```

However, when a string contains words, numbers, or special characters (a substring) that are also enclosed in quotation marks, you should use a different style. For example, if a substring use double quotation marks, enclose the entire string in single quotation marks, as shown here:

```python
'The "near side" is a part of the Moon that faces the Earth.'
```

Similarly, if there are single quotation marks (or an apostrophe, as in Moon's in the following example) anywhere within the string, enclose the entire string in double quotation marks:

```python
"We only see about 60% of the Moon's surface."
```

Failure to alternate single and double quotation marks can cause the Python interpreter to raise a syntax error, as show here:

```python
'We only see about 60% of the Moon's surface.'
File "<stdin>", line 1
    'We only see about 60% of the Moon's surface.'
                                       ^
SyntaxError: invalid syntax
```

When the text has a combination of single and double quotation marks, you can use triple quotation marks to prevent problems with the interpreter:

```python
"""We only see about 60% of the Moon's surface, this is known as the "near side"."""
```

## Multiline text

There are a few different ways to define multiple lines of text as a single variable. The most common ways are:

* Use a newline character (`\n`).
* Use triple quotation marks (""").

Newline characters separate the text into multiple lines when you print the output:

```python
multiline =  "Facts about the Moon: \n There is no atmosphere. \n There is no sound."
print(multiline)
```

```
Facts about the Moon:
    There is no atmosphere.
    There is no sound.
```

You can achieve the same result by using triple quotation marks:

```python
multiline = """ Facts about the Moon:
    There is no atmosphere.
    There is no sound. """
print(multiline)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.pqtkha.com/programming/technology/python/microsoft-training-python-for-beginners/module-3-use-strings-in-python/string-basics-in-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
