# String format in Python

Besides transforming text and performing basic operations, such as matching and searching, it's essential to format the text when you're presenting information. The simplest way to present text information with Python is to use the `print()` function. You'll find it critical to get information in variables and other data structures into strings that `print()` can use.&#x20;

In this unit, you'll learn several valid ways to include variable values in text by using Python.

## Percent sign (`%`) formatting

The placeholder for the variable in the string is `%s`. After the string, use another `%` character followed by the variable name. The following example shows how to format by using the `%` character:

{% code overflow="wrap" %}

```python
mass_percentage = "1/6"
print("On the Moon, you would weigh about %s of your weight on Earth." %mass_percentage)
```

{% endcode %}

Output: `On the Moon, you should weigh about 1/6 of your weight on Earth.`

Using multiple values changes the syntax, because it requires parentheses to surround the variables that are passed in:

{% code overflow="wrap" %}

```python
print("""Both sides of the %s get the same amount of sunlight, but only one side is seen from %s because the %s rotates around its own axis when it orbits %s.""" %("Moon", "Earth", "Moon", "Earth"))
```

{% endcode %}

Output: `Both sides of the Moon get the same amount of sunlight, but only one side is seen from Earth because the Moon rotates around its own axis when it orbits Earth.`

{% hint style="info" %}
Tip

Although this method is still a valid way format strings, it can lead to errors and decreased clarity in code when you're dealing with multiple variables. Either of the other two formatting options described in this unit would be better suited to this purpose.
{% endhint %}

## The `format()` method

The `.format()` method uses braces (`{}`) as placeholders within a string, and it uses variable assignment for replacing text.

{% code overflow="wrap" %}

```python
mass_percentage = "1/6"
print("On the Moon, you would weigh about {} of your weight on Earth.".format(mass_percentage))
```

{% endcode %}

Output: `On the Moon, you would weigh about 1/6 of your weight on Earth.`

You don't need to assign repeated variables multiple times, making it less verbose because fewer variables need to assigned:

{% code overflow="wrap" %}

```python
mass_percentage = "1/6"
print("""You are lighter on the {0}, because on the {0} you would weigh about {1} of your weight on Earth.""".format("Moon", mass_percenatage))
```

{% endcode %}

Output: `On the Moon, you would weigh about 1/6 of your weight on Earth.`

Instead of empty braces, the substitution is to use numbers. The `{0}` means to use the first (index of zero) argument to `.format()`, which in this case is Moon. For simple repetition `{0}` works well, but it reduces readability. To impro readability use keywork arguments in `.format()` and then reference the same arguments within braces:

{% code overflow="wrap" %}

```python
mass_percentage = "1/6"
print("""You are lighter on the {moon}, because on the {moon} you would weigh about {mass} of your weight on Earth.""".format(moon="Moon", mass=mass_percentage))
```

{% endcode %}

Output: `You are lighter on the Moon, because on the Moon you would weigh about 1/6 of your weight on Earth.`

## About f-string

As of Python version 3.6, it's possible to use *f-strings*. These strings look like templates and use the variable names from your code. Using f-strings in the preceding example would look like this:

{% code overflow="wrap" %}

```python
print(f"On the Moon, you would weigh about {mass_percentage} of your weight on Earth.")
```

{% endcode %}

Output: `On the Moon, you would weigh about 1/6 of your weight on Earth.`&#x20;

The variables go within braces, and the string must use the `f` prefix.

Aside form f-strings being less verbose than any other formatting option, it's possible to use expressions with braces. These expressions can be functions or direct operations. For example, if you want to represent the `1/6` value as a percentage with one decimal place, you can you the `round()` function directly:

```python
print(round(100/6, 1))
```

Output: `16.7`

With f-strings, you don't need to assign a value to a variable beforehand:

{% code overflow="wrap" %}

```python
print(f"On the Moon, you would weigh about {round(100/6,1)}% of your weight on Earth.")
```

{% endcode %}

Output: `On the Moon, you would weigh about 16.7% of your weight on Earth.`

Using an expression doesn't require a function call. Any of the string methods are valid as well. For example, the string could enforce a specific casing for creating a heading:

```python
subject = "interesting facts about the moon"
heading = f"{subject.tile()}"
print(heading)
```

Output: `Interesting Facts About The Moon`


---

# 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-format-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.
