String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text.
custom_string = "String formatting"
print(f"{custom_string} is a powerful technique")
String formatting is a powerful technique
As a data scientist, you would use it for inserting a title in a graph, show a message or an error, or pass a statement to a function.
- Positional formatting
- Formatted string literals
- Template method
We put placeholders defined by a pair of curly braces in a text. We call the string dot format method. Then, we pass the desired value into the method. The method replaces the placeholders using the values in the order of appearance replace by value:
We define a string and insert two placeholders. We pass two strings to the method, which will be passed to get the following output:
print("Machine learning provides {} the ability to learn {}".format("systems", "automatically"))
Machine learning provides systems the ability to learn automatically
We can use variables for both the string and the values passed to the method. In the below example code, we define a string with placeholders and two other variables. We apply the format method to the string using the two defined variables. The method reads the string and replaces the placeholders with the given values.
my_string = "{} rely on {} datasets"
method = "Supervised algorithms"
condition = "labeled"
print(my_string.format(method, condition))
Supervised algorithms rely on labeled datasets
Reordering Values
In the below example, you add index numbers into the placeholders to reorder values. This affects the order in which the method replaces the placeholders.
The method replaces them with the values in the given order.
print("{} has a friend called {} and a sister called {}". format("Betty", "Linda", "Daisy"))
String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text.
As a data scientist, you would use it for inserting a title in a graph, show a message or an error, or pass a statement to a function.
Methods for Formatting
String.Format Method
We put placeholders defined by a pair of curly braces in a text. We call the string dot format method. Then, we pass the desired value into the method. The method replaces the placeholders using the values in the order of appearance replace by value:
Positional Formatting
We define a string and insert two placeholders. We pass two strings to the method, which will be passed to get the following output:
We can use variables for both the string and the values passed to the method. In the below example code, we define a string with placeholders and two other variables. We apply the format method to the string using the two defined variables. The method reads the string and replaces the placeholders with the given values.
Reordering Values
In the below example, you add index numbers into the placeholders to reorder values. This affects the order in which the method replaces the placeholders.
The method replaces them with the values in the given order.