String Formatting#
Introduction#
f-strings are the recommended way to format text and numbers in Python. They allow variables, expressions, and function calls to be embedded directly inside {}
.
To create an f-string, add f
before the string, and place any variable, expression, or function call inside {}
.
f-Strings – Embed variables and expressions directly in text.
f"{value}"
– Displays variables, expressions, or function calls inside a string.
Formatting Numbers in f-strings – Control how numeric values appear.
:.xf
– Sets the number of decimal places to show.round
– Rounds a number to a given number of decimal places.:,
– Adds commas as thousands separators.:.x%
– Converts a decimal to a percentage with x decimal places.:#x
– Formats a number as hexadecimal.:b
– Formats a number as binary.
String Combination – Combine text and values.
f"{value}"
– Combine strings and variables in a single expression.+
operator – Concatenate strings manually with optional type conversion.
String Methods – Change the case of text.
Substring Checks – Test for presence or position of text.
in
– Checks if a word exists in a string.startswith()
– Checks if a string begins with a given value.endswith()
– Checks if a string ends with a given value.
Escape Sequences – Format output with special characters.
Formatting Numbers in f-strings#
f-strings allow precise control over decimal places, rounding, thousands separators, and more by using the following format specifiers:
Fixed Decimal Places#
.xf
controls how many decimal places a number is displayed with.
Usage:
.xf
Parameters |
Description |
---|---|
|
The amount of decimal places to show. |
# Example coming soon
Rounding Numbers#
round
rounds numbers outside of an f-string or inside of the {}
.
Usage:
round(number, x)
Parameters |
Description |
---|---|
|
The number to round. |
|
The amount of decimal places to round to. |
# Example coming soon
Thousands Separator#
,
inserts commas as thousands separators to make large numbers more readable.
Usage:
,
Parameters |
Description |
---|---|
This format specifier has no parameters. |
# Example coming soon
Percentage#
.x%
formats decimal values as percentages.
Usage:
.x%
Parameters |
Description |
---|---|
|
The amount of decimal places to show. |
# Example coming soon
Hexadecimal#
.#x
converts numbers to hexadecimal.
Usage:
.#x
Parameters |
Description |
---|---|
This format specifier has no parameters. |
# Example coming soon
Binary#
b
converts numbers to binary (base 2).
Usage:
b
Parameters |
Description |
---|---|
This format specifier has no parameters. |
# Example coming soon
Combining Strings#
You can combine (or concatenate) strings using two approaches:
Using f-strings#
With f-strings, you can embed variables directly inside {}
.
# Example coming soon
+ Operator#
You can combine strings manually using the +
operator.
Note: Non-strings must first be converted to strings using str()
.
# Example coming soon
String Methods#
Python provides built-in methods for modifying and checking strings.
upper#
upper
converts all letters in a string to uppercase.
Usage:
upper()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
lower#
lower
converts all letters in a string to lowercase.
Usage:
lower()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
Checking for Substrings#
in#
in
is a keyword that checks if a word exists in a string.
# Example coming soon
startswith#
startswith
returns if a string starts with a specific word. This method returns a Boolean value:
True
- The word starts the string.False
- The word does not start the string.
Usage:
startswith(substring)
Parameters |
Description |
---|---|
|
The substring to check inside the string. |
# Example coming soon
endswith#
endswith
returns if a string ends with a specific word. This method returns a Boolean value:
True
- The word ends the string.False
- The word does not end the string.
Usage:
startswith(substring)
Parameters |
Description |
---|---|
|
The substring to check inside the string. |
# Example coming soon
Escape Sequences#
Escape sequences are special characters used inside strings to format text output. They are only available for use with the Console.
New Line#
\n
moves text to a new line when printing.
# Display text on two lines
print("First line\nSecond line")
Tab Spacing#
\t
inserts a tab space between words or numbers,
# Display the quantity of barrels
quantity = 2
print("Barrels:\t", quantity)