Python String format

python string format

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. today in this example below we will learn about Python String format.

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.

Best Python Learning Books from Pearson : Python books from Pearson

Python String format 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.

so 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

Let’s define a string and insert two placeholders.

as 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

Now, we can use variables for both the string and the values passed to the method.

also In the below example code, we define a string with placeholders and two other variables.

Because We apply the format method to the string using the two defined variables.

so, 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

Python String format 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.

Because 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

Python String format 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"}

Because 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 the 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!

so, 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

Try it for yourself.

To learn more about positional formatting, please see this video from our course, Regular Expressions in Python.

This content is taken from DataCamp’s Regular Expressions in Python course by Maria Eugenia Inzaugarat.



Here are some Python string formatting examples using the format() method:

  1. Basic String Formatting:
pythonCopy codename = "Alice"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
  1. Specifying Index Positions:
pythonCopy codename = "Bob"
age = 25
print("My name is {0} and I am {1} years old. {0} is a nice name!".format(name, age))
  1. Named Placeholders:
pythonCopy codeperson = {'name': 'Charlie', 'age': 28}
print("My name is {name} and I am {age} years old.".format(**person))
  1. Formatting Numbers with Precision:
pythonCopy codepi = 3.14159265359
print("The value of pi is {:.2f}".format(pi))
  1. Formatting with Alignment and Width:
pythonCopy codemessage = "Hello"
print("{:^20}".format(message))  # Center aligned in a width of 20 characters
print("{:>20}".format(message))  # Right aligned in a width of 20 characters
print("{:<20}".format(message))  # Left aligned in a width of 20 characters
  1. Formatting Integers and Padding:
pythonCopy codenumber = 42
print("The answer is {:05d}".format(number))  # Padded with zeros to a width of 5 characters
  1. Formatting with Named Arguments (Python 3.6+):
pythonCopy codename = "David"
age = 22
print(f"My name is {name} and I am {age} years old.")
  1. Formatting Date and Time (Python 3.2+):
pythonCopy codefrom datetime import datetime
now = datetime.now()
print("Current date and time: {:%Y-%m-%d %H:%M:%S}".format(now))

These examples showcase various ways to use the format() method to format strings in Python. String formatting can be a powerful tool for creating well-structured and informative output.

Regenerate