{"id":189973,"date":"2026-04-03T10:07:28","date_gmt":"2026-04-03T10:07:28","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=189973"},"modified":"2026-04-03T10:50:41","modified_gmt":"2026-04-03T10:50:41","slug":"micropython-string-formatting","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-string-formatting\/","title":{"rendered":"MicroPython String Formatting &#8211; 3 Different Ways (ESP32, ESP8266, RPi Pico)"},"content":{"rendered":"\n<p>In this quick guide, we&#8217;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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/Micropython-string-formatting.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython String Formatting - 3 Different Ways Raspberry Pi Pico ESP32 ESP8266 NodeMCU\" class=\"wp-image-189978\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/Micropython-string-formatting.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/Micropython-string-formatting.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/Micropython-string-formatting.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/Micropython-string-formatting.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/Micropython-string-formatting.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>Table of Contents:<\/strong><\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll cover the following subjects:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#string-formatting\" title=\"\">MicroPython String Formatting<\/a><\/li>\n\n\n\n<li><a href=\"#percent-operator\" title=\"\">% Operator for String Formatting<\/a><\/li>\n\n\n\n<li><a href=\"#format-method\" title=\"\"><em>format()<\/em> Method for String Formatting<\/a><\/li>\n\n\n\n<li><a href=\"#f-strings\" title=\"\">F-strings (Formatted String Literals)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"string-formatting\">MicroPython String Formatting<\/h2>\n\n\n\n<p>MicroPython offers several methods for string formatting, each with its own syntax and features. The most common methods include the <strong>% operator<\/strong>, the <span class=\"rnthl rntliteral\">format()<\/span><strong> method<\/strong>, and <strong>f-strings<\/strong> (also called<em> formatted string literals<\/em>). In the following sections, we\u2019ll take a look at each of these methods and provide examples of how to use them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"percent-operator\">% Operator for String Formatting<\/h2>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">%<\/span> operator in MicroPython allows you to perform string formatting by replacing placeholders in a string with corresponding values. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>name = \"Sara\"\nage = 30\n# Format a string with placeholders\nheight = 160.521\nmessage = \"Hello, my name is %s. I am %d years old and I'm %.1f cm tall.\" % (name, age, height)\n\n# Print the formatted message\nprint(message)<\/code><\/pre>\n\n\n\n<p>This generates the following output.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"779\" height=\"678\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/percent-formatting-example-micropython.png?resize=779%2C678&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Micropython String formatting example\" class=\"wp-image-189976\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/percent-formatting-example-micropython.png?w=779&amp;quality=100&amp;strip=all&amp;ssl=1 779w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/percent-formatting-example-micropython.png?resize=300%2C261&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/percent-formatting-example-micropython.png?resize=768%2C668&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 779px) 100vw, 779px\" \/><\/figure><\/div>\n\n\n<p>In this example, <span class=\"rnthl rntliteral\">%s<\/span>, <span class=\"rnthl rntliteral\">%d<\/span>, and <span class=\"rnthl rntliteral\">%.1f<\/span> 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.<\/p>\n\n\n\n<p>If you have a variable that is of type float, there are more formatting options. In this case, the <span class=\"rnthl rntliteral\">%.1f<\/span> syntax is used to specify the precision of the float value. The number after the dot (<span class=\"rnthl rntliteral\">.<\/span>) represents the number of decimal places to display in the formatted float.<\/p>\n\n\n\n<p>More about this method and conversion types <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#old-string-formatting\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"format-method\"><em>format()<\/em> Method for String Formatting<\/h2>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">format()<\/span> method provides a more versatile and flexible approach compared to the <span class=\"rnthl rntliteral\">%<\/span> operator. It allows you to specify placeholders using named indexes <span class=\"rnthl rntliteral\">{name}<\/span>, numbered indexes <span class=\"rnthl rntliteral\">{0}<\/span>, or even empty placeholders <span class=\"rnthl rntliteral\">{}<\/span>. See the example below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Different types of placeholders\nmessage1 = \"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 )\n\nmessage2 = \"Hello, my name is {0}. I am {1} years old and I'm {2:.2f} cm tall.\".format(\"Sara\",30, 160.521)\n\nmessage3 = \"Hello, my name is {}. I am {} years old and I'm {:.2f} cm tall.\".format(\"Sara\",30, 160.521)\n\n# Print the formatted message\nprint(message1)\nprint(message2)\nprint(message3)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f-strings\">F-strings (Formatted String Literals)<\/h2>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>name = \"Sara\"\nage = 30\nheight = 160.521\n\n# Format a string with placeholders\nmessage = f\"Hello, my name is {name}. I am {age} years old and I'm {height:.1f} cm tall.\"\n\n# Print the formatted message\nprint(message)<\/code><\/pre>\n\n\n\n<p>In this example, <span class=\"rnthl rntliteral\">{name}<\/span>, <span class=\"rnthl rntliteral\">{age}<\/span>, and <span class=\"rnthl rntliteral\">{height:.1f}<\/span> 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 <span class=\"rnthl rntliteral\">f<\/span> or <span class=\"rnthl rntliteral\">F<\/span> before the <span class=\"rnthl rntliteral\">&#8220;<\/span>.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, we provided you with three different methods for string formatting in MicroPython. Which one is your favorite?<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. You can put this into practice in your ESP32, ESP8266, or Raspberry Pi Pico MicroPython projects.<\/p>\n\n\n\n<p>More MicroPython resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico\/Pico W with MicroPython eBook<\/a><\/strong> <\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\" title=\"\"><strong>MicroPython Programming with ESP32 and ESP8266<\/strong> <strong>eBook<\/strong><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-esp8266-micropython\/\" title=\"\">All our ESP32\/ESP8266 MicroPython Projects<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\">All our Raspberry Pi Pico Projects (mostly MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this quick guide, we&#8217;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, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython String Formatting &#8211; 3 Different Ways (ESP32, ESP8266, RPi Pico)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-string-formatting\/#more-189973\" aria-label=\"Read more about MicroPython String Formatting &#8211; 3 Different Ways (ESP32, ESP8266, RPi Pico)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":189978,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[310,309,264],"tags":[],"class_list":["post-189973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2026\/02\/Micropython-string-formatting.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/189973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=189973"}],"version-history":[{"count":8,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/189973\/revisions"}],"predecessor-version":[{"id":193784,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/189973\/revisions\/193784"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/189978"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=189973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=189973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=189973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}