Tutorials
python

# Python String format()

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.

## Methods for formatting

• Positional formatting
• Formatted string literals
• Template method

### 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:

'text{}'.format(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:

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"))

Betty has a friend called Linda and a sister called Daisy


If we add the index numbers, the replacement order changes accordingly.

print("{2} has a friend called {0} and a sister called {1}". format("Betty", "Linda", "Daisy"))

Daisy has a friend called Betty and a sister called Linda


## Name Placeholders

We can also introduce keyword arguments that are called by their keyword name.

In the example code below, we inserted keywords in the placeholders. Then, we call these keywords in the format method. We then assign which variable will be passed for each of them, resulting in the following output.

tool="Unsupervised algorithms"
goal="patterns"
print("{title} try to find {aim} in the dataset".format(title=tool, aim=goal))

Unsupervised algorithms try to find patterns in the dataset


Let's examine this code below. We have defined a dictionary with keys: tool and goal.

my_methods = {"tool": "Unsupervised algorithms", "goal": "patterns"}


We want to insert their values in a string. Inside the placeholders, we can specify the value associated with the key tool of the variable data using bracket notation. Data is the dictionary specified in the method, and tool is the key present in that dictionary.

print('{data[tool]} try to find {data[goal]} in the dataset'.format(data=my_methods))


So, we get the desired output shown below. Be careful! You need to specify the index without using quotes.

Unsupervised algorithms try to find patterns in the dataset


## Format Specifier

We can also specify the format specifies inside curly braces. This defines how individual values are presented. Here, we’ll use the syntax index colon specifier. One of the most common format specifiers is float represented by f. In the code, we specify that the value passed with the index 0 will be a float.

print("Only {0:f}% of the {1} produced worldwide is {2}!". format(0.5155675, "data", "analyzed"))

Only 0.515567% of the data produced worldwide is analyzed!


We could also add .2f indicating that we want the float to have two decimals, as seen in the resulting output.

print("Only {0:.2f}% of the {1} produced worldwide is {2}!".format(0.5155675, "data", "analyzed"))

Only 0.52% of the data produced worldwide is analyzed!


## Formatting datetime

Python has a module called datetime that allows us to, for example, to get the time and date for today.

from datetime import datetime
print(datetime.now())

2020-08-08 06:28:42.715243


But since the format returned is very particular, you could use the format specifier such as %y-%m-%d-%h-%m to adjust the format to something more familiar to us!

print("Today's date is {:%Y-%m-%d %H:%M}".format(datetime.now()))

Today's date is 2020-08-08 06:29


## Interactive Example

In the following example, you will assign the substrings going from the 4th to the 19th character, and from the 22nd to the 44th character of wikipedia_article to the variables first_pos and second_pos, respectively. Adjust the strings so they are lowercase. Finally, print the variables first_pos and second_pos.

# Assign the substrings to the variables
first_pos = wikipedia_article[3:19].lower()
second_pos = wikipedia_article[21:44].lower()


When we run the above code, it produces the following result:

computer science artificial intelligence