top of page

Python and Data Science: Comprehensive Tutorial on String Formatting


1. Introduction to String Formatting


String formatting is a core concept in Python and is used to control how and where data gets inserted into a string. It's akin to plugging in variables into an equation or filling in the blanks in a sentence. This process is used when you want to print out information, and you want it to look a certain way or to adhere to a specific format.


For instance, consider an analogy of a "Mad Libs" game, where you have to fill in specific types of words (nouns, adjectives, verbs, etc.) into a pre-structured sentence to create a funny story. String formatting operates similarly, only that it's not always humorous.


In the realm of data science, string formatting is used to print readable, well-structured, and formatted results, so we can clearly understand the output of our computations, especially when dealing with complex data structures and extensive computational results.

# Simple string formatting example
name = "Alice"
age = 25
print("My name is %s and I'm %d years old." % (name, age))

Output:

My name is Alice and I'm 25 years old.


2. String Formatting Methods in Python


Python provides three main approaches to string formatting: positional formatting, formatted string literals or f-strings, and template methods.

Imagine you are the director of a play and your actors are your data. You have to decide where each actor should stand (positional formatting), how they should express their lines (formatted string literals), or maybe you want to use a pre-existing script with fill-in-the-blank dialogues (template methods).


The method you choose depends on your specific requirements and familiarity with each method.


3. Positional Formatting


Positional formatting refers to the process where variables or values are placed in a specific position in a string using a placeholder. Like in a treasure map, where 'X' marks the spot for treasure, we use '%s' or '%d' to mark the spot for a string or an integer, respectively.


Let's look at a practical example of positional formatting:

# Positional formatting example
name = "Alice"
age = 25
print("My name is %s and I'm %d years old." % (name, age))

Output:

My name is Alice and I'm 25 years old.

In the above example, '%s' is a placeholder for the string (name), and '%d' is a placeholder for the integer (age). The variables in the parentheses following the '%' are substituted in order into the placeholders in the string.


4. Advanced Positional Formatting


Beyond the basics, there are a host of other features available in positional formatting.


Concept and Procedure of Reordering Values


In positional formatting, we can reorder the position of variables within a string by indicating their position in the placeholder. Consider a stage where actors switch roles; positional formatting allows for this versatility.

# Reordering values
print("I'm %d years old and my name is %s." % (age, name))

Output:

I'm 25 years old and my name is Alice.


Introduction to Named Placeholders and Their Use


Named placeholders allow us to use variables by their names for substitutions. This could be compared to a role call where every actor responds when their name is called.

# Named placeholders
print("My name is %(n)s and I'm %(a)d years old." % {"n": name, "a": age})

Output:

My name is Alice and I'm 25 years old.


Using Dictionary Keys for Positional Formatting


We can use dictionary keys in positional formatting. Here, the keys from the dictionary are used as the variable names in the string formatting.

# Dictionary keys
info = {"name": "Alice", "age": 25}
print("My name is %(name)s and I'm %(age)d years old." % info)

Output:

My name is Alice and I'm 25 years old.


Introduction and Usage of Format Specifiers


Format specifiers are used to control the presentation of a value. This could include precision in floating-point numbers, width of fields, alignment of values, and so on. This is much like setting the stage properties for our play.

# Format specifiers
print("Price of the book is: %.2f" % 45.6789)

Output:

Price of the book is: 45.68


Formatting datetime Using Positional Formatting


For datetime objects, we can control how the date and time are presented using format specifiers.

from datetime import datetime
now = datetime.now()

print("Current date and time: %s" % now.strftime("%Y-%m-%d %H:%M:%S"))

Output:

Current date and time: 2023-08-02 15:30:00


5. Formatted String Literals (f-strings)


Formatted string literals, or f-strings, are a newer way to format strings in Python 3.6 and above. They're prefixed with an 'f' and use curly braces {} to enclose the variables. This is much like a modern play script where instructions are clearly marked for easy reading.

# Basic f-string
name = "Alice"
age = 25
print(f"My name is {name} and I'm {age} years old.")

Output:

My name is Alice and I'm 25 years old.


Type Conversion in f-strings


In f-strings, we can also perform type conversions. This is similar to an actor assuming a different role or portraying a different character in a play.

# Type conversion
number = 12345
print(f"The number as an integer: {number!s}")

Output:

The number as an integer: 12345


Format Specifiers in f-strings


Just as with positional formatting, f-strings also support format specifiers, providing a high level of control over the string's appearance.

# Format specifiers
price = 45.6789
print(f"Price of the book is: {price:.2f}")

Output:

Price of the book is: 45.68


Formatting datetime Using f-strings


Datetime formatting is also supported in f-strings. Here, we can specify the format in which we want to display the date and time.

from datetime import datetime
now = datetime.now()

print(f"Current date and time: {now:%Y-%m-%d %H:%M:%S}")

Output:

Current date and time: 2023-08-02 15:30:00


Dictionary Key Lookups in f-strings


F-strings also allow for dictionary key lookups. This is like a role call where each actor responds when their role is announced.

# Dictionary keys
info = {"name": "Alice", "age": 25}
print(f"My name is {info['name']} and I'm {info['age']} years old.")

Output:

My name is Alice and I'm 25 years old.


Introduction to Escape Sequences in f-strings


Escape sequences are used to insert special characters into strings. For instance, to include a literal curly brace {} in an f-string, we can use double curly braces.

# Escape sequences
print(f"My name is {{name}} and I'm {{age}} years old.")

Output:

My name is {name} and I'm {age} years old.


Inline Operations in f-strings


F-strings allow for inline expressions or operations, which get evaluated at runtime. This is like a spontaneous improv scene in the middle of a scripted play.

# Inline operations
x = 5
y = 10
print(f"The sum of {x} and {y} is {x + y}.")

Output:

The sum of 5 and 10 is 15.


Function Calls within f-string Expressions


You can even call functions directly within f-string expressions!

# Function calls
def greet(name):
    return f"Hello, {name}!"

print(f"{greet('Alice')}")

Output:

Hello, Alice!


6. Template Strings


Template strings provide a less complex way to handle string formatting. They're particularly helpful when dealing with user-provided format strings.


Introduction to Template Strings and Their Typical Use Cases


Template strings are like script templates for a theater play. They are simple and easy to use but offer less flexibility. They can be particularly useful when the formatting information is coming from an external source such as user input. This could prevent potential security issues, as the string format is fixed and will not be evaluated for Python expressions.

from string import Template

t = Template("Hello, $name!")
print(t.substitute(name="Alice"))

Output:

Hello, Alice!


Basic Syntax of Template Strings


The syntax of template strings involves the dollar sign ($) followed by the identifier. It's like a stage direction telling which actor should play next.

from string import Template

t = Template("$x times $y is $z")
print(t.substitute(x=5, y=10, z=50))

Output:

5 times 10 is 50


Substituting Multiple Identifiers in Template Strings


Just like multiple actors playing in a scene, template strings can handle multiple identifiers as well.

from string import Template

t = Template("$greeting, I'm $name.")
print(t.substitute(greeting="Hello", name="Alice"))

Output:

Hello, I'm Alice.


Using Extra Curly Braces to Enclose Identifiers in Template Strings


When we need to use more complex expressions with template strings, we can enclose them within curly braces {}. This is like setting a specific spot on the stage for an actor to perform.

from string import Template

t = Template("${greeting}, I'm ${name}.")
print(t.substitute(greeting="Hello", name="Alice"))

Output:

Hello, I'm Alice.


Escaping Dollar Signs in Template Strings


Just like escaping curly braces in f-strings, we need to escape the dollar sign ($) in template strings by using two dollar signs ($$).

from string import Template

t = Template("It costs $$${price}.")
print(t.substitute(price=100))

Output:

It costs $100.


Using Try-Except Blocks to Handle Exceptions in Template Strings


Just as a stage manager would step in if something goes wrong during a performance, we can use try-except blocks to handle any exceptions that might occur during string formatting.

from string import Template

try:
    t = Template("Hello, $name!")
    print(t.substitute())
except KeyError:
    print("Missing information!")

Output:

Missing information!


Introduction to Safe Substitution in Template Strings


Template strings provide a method, safe_substitute(), that leaves placeholders

unchanged if data for substitution isn't provided. It's like a role being left empty when an actor is missing.

from string import Template

t = Template("Hello, $name!")
print(t.safe_substitute())

Output:

Hello, $name!


7. Choosing the Right String Formatting Method


The right string formatting method to use depends on your requirements and the Python version you're using. Think of this like choosing the right actor for a role based on the character's requirements and the actor's abilities.

  • If you're using Python 3.6 or later, and need a versatile and easy-to-use formatting method, go for f-strings.

  • If your code needs to be compatible with Python versions earlier than 3.6, or if you're dealing with more complex formatting requirements, use positional formatting.

  • If you're handling user-provided format strings, to avoid potential security issues, consider using template strings.


And that's the end of our Python and Data Science tutorial on String Formatting! We've gone through various methods and their uses, alongside plenty of code examples for practical understanding. Hope this has given you a clearer understanding of how to use string formatting in Python, and how it can benefit your data science journey!

bottom of page