MicroPython String Formatting – 3 Different Ways (ESP32, ESP8266, RPi Pico)

In this quick guide, we’ll explore different string formatting techniques essential for manipulating text in MicroPython. These techniques are useful for tasks such as displaying information to users, logging data, or formatting output for storage or communication. This can be applied to your ESP32, ESP8266, or Raspberry Pi Pico MicroPython projects.

MicroPython String Formatting - 3 Different Ways Raspberry Pi Pico ESP32 ESP8266 NodeMCU

For example: concatenate a string with a number, format the number to display only two decimal places, populate a text with different variables, and more.

Table of Contents:

In this tutorial, we’ll cover the following subjects:

MicroPython String Formatting

MicroPython offers several methods for string formatting, each with its own syntax and features. The most common methods include the % operator, the format() method, and f-strings (also called formatted string literals). In the following sections, we’ll take a look at each of these methods and provide examples of how to use them.

% Operator for String Formatting

The % operator in MicroPython allows you to perform string formatting by replacing placeholders in a string with corresponding values. For example:

name = "Sara"
age = 30
# Format a string with placeholders
height = 160.521
message = "Hello, my name is %s. I am %d years old and I'm %.1f cm tall." % (name, age, height)

# Print the formatted message
print(message)

This generates the following output.

Micropython String formatting example

In this example, %s, %d, and %.1f are placeholders for a string, an integer, and a floating-point number, respectively. The values (name, age, height) are substituted into the placeholders in the order they appear.

If you have a variable that is of type float, there are more formatting options. In this case, the %.1f syntax is used to specify the precision of the float value. The number after the dot (.) represents the number of decimal places to display in the formatted float.

More about this method and conversion types here.

format() Method for String Formatting

The format() method provides a more versatile and flexible approach compared to the % operator. It allows you to specify placeholders using named indexes {name}, numbered indexes {0}, or even empty placeholders {}. See the example below:

# Different types of placeholders
message1 = "Hello, my name is {name}. I am {age} years old and I'm {height:.2f} cm tall.".format(name ="Sara",age=30, height=160.521 )

message2 = "Hello, my name is {0}. I am {1} years old and I'm {2:.2f} cm tall.".format("Sara",30, 160.521)

message3 = "Hello, my name is {}. I am {} years old and I'm {:.2f} cm tall.".format("Sara",30, 160.521)

# Print the formatted message
print(message1)
print(message2)
print(message3)

F-strings (Formatted String Literals)

F-strings are relatively recent and provide a concise and readable syntax for string formatting. They allow you to embed expressions directly within string literals automatically. For example:

name = "Sara"
age = 30
height = 160.521

# Format a string with placeholders
message = f"Hello, my name is {name}. I am {age} years old and I'm {height:.1f} cm tall."

# Print the formatted message
print(message)

In this example, {name}, {age}, and {height:.1f} are placeholders for a string, an integer, and a floating-point number with one decimal place, respectively. An f-string is created by adding a string literal with the letter f or F before the .

When an f-string is evaluated, Python/MicroPython evaluates the expressions enclosed within curly braces and replaces them with their values. This allows you to include variable values, function calls, and even arithmetic expressions directly within the string.

Wrapping Up

In this tutorial, we provided you with three different methods for string formatting in MicroPython. Which one is your favorite?

We hope you’ve found this tutorial useful. You can put this into practice in your ESP32, ESP8266, or Raspberry Pi Pico MicroPython projects.

More MicroPython resources:



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.