{"id":113791,"date":"2026-06-05T23:36:32","date_gmt":"2026-06-05T18:06:32","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=113791"},"modified":"2026-06-05T23:37:32","modified_gmt":"2026-06-05T18:07:32","slug":"how-to-use-python-f-strings-with-examples","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-use-python-f-strings-with-examples\/","title":{"rendered":"How to Use Python f-Strings With Examples\u00a0"},"content":{"rendered":"\n<p>Python offers several ways to format strings, but f-strings are among the simplest and most readable methods. They let developers easily insert variables, expressions, and calculations directly into a string without complicating the code.<\/p>\n\n\n\n<p>If you are learning Python for data science, automation, backend development, or machine learning, knowing about f-strings is important because they are commonly used in actual Python projects.<\/p>\n\n\n\n<p>In this blog, you will discover what f strings are in Python, how they work, their syntax, benefits, and various examples beginners should try.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL:DR<\/strong><\/h2>\n\n\n\n<ol>\n<li>Python f-strings are a modern way to format strings using variables and expressions directly within text.<\/li>\n\n\n\n<li>They were introduced in Python 3.6 and are faster and clearer than older formatting methods.<\/li>\n\n\n\n<li>f strings use curly braces {} to insert variables, calculations, and function outputs.<\/li>\n\n\n\n<li>They help beginners write cleaner and shorter Python code.<\/li>\n\n\n\n<li>f strings are widely applied in real-world Python applications, including automation, web development, and data science.<\/li>\n<\/ol>\n\n\n\n<div class=\"guvi-answer-card\" style=\"margin: 40px 0;\">\n\n  <div style=\"\n    position: relative;\n    background: linear-gradient(135deg, #f0fff4, #e6f7ee);\n    border: 1px solid #cfeedd;\n    padding: 26px 24px 22px 24px;\n    border-radius: 14px;\n    font-family: Arial, sans-serif;\n    box-shadow: 0 6px 16px rgba(0,0,0,0.05);\n  \">\n\n    <!-- Top accent -->\n    <div style=\"\n      position: absolute;\n      top: 0;\n      left: 0;\n      height: 6px;\n      width: 100%;\n      background: linear-gradient(to right, #099f4e, #6dd5a3);\n      border-radius: 14px 14px 0 0;\n    \"><\/div>\n\n    <!-- Title -->\n    <h3 style=\"\n      margin: 10px 0 12px 0;\n      color: #099f4e;\n      font-size: 20px;\n    \">\n      What Are f-Strings in Python?\n    <\/h3>\n\n    <!-- Content -->\n    <p style=\"\n      margin: 0;\n      color: #2f4f3f;\n      font-size: 16px;\n      line-height: 1.7;\n    \">\n      f-strings (formatted string literals) are a string formatting feature introduced in Python 3.6 that allows variables, expressions, and calculations to be embedded directly inside strings using curly braces <code>{}<\/code>. By placing the letter <code>f<\/code> before a string, Python evaluates the expressions within the braces and inserts their values into the final output. Compared to older formatting methods such as <code>%<\/code> formatting and <code>str.format()<\/code>, f-strings are more concise, readable, and efficient, making them the preferred approach for string formatting in modern Python.\n    <\/p>\n\n  <\/div>\n\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Use f-Strings?<\/strong><\/h2>\n\n\n\n<p>Before <a href=\"https:\/\/www.guvi.in\/blog\/f-strings-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">f-strings <\/a>became available, developers mostly used string concatenation or the format() method.<\/p>\n\n\n\n<p>Here is an example using concatenation:<\/p>\n\n\n\n<p>name = &#8220;Harini&#8221;<br>print(&#8220;Hello &#8221; + name)<\/p>\n\n\n\n<p>Example using format():<\/p>\n\n\n\n<p>name = &#8220;Harini&#8221;<br>print(&#8220;Hello {}&#8221;.format(name))<\/p>\n\n\n\n<p>Example using f string:<\/p>\n\n\n\n<p>name = &#8220;Harini&#8221;<br>print(f&#8221;Hello, {name}&#8221;)<\/p>\n\n\n\n<p>The f-string version looks more natural and is easier to read.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Syntax of Python f-Strings<\/strong><\/h2>\n\n\n\n<p>The syntax of f-strings is straightforward:<\/p>\n\n\n\n<p>f&#8221;text {variable}&#8221;<\/p>\n\n\n\n<p>You can place <a href=\"https:\/\/www.guvi.in\/hub\/python\/variables-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">variables<\/a>, expressions, or even function calls inside curly braces.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<p>name = &#8220;Python&#8221;<br>version = 3.13<br>print(f&#8221;{name} version is {version}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Python version is 3.13<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic f String Examples<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1: Printing a Name<\/strong><\/h3>\n\n\n\n<p>name = &#8220;Rahul&#8221;<br>print(f&#8221;My name is {name}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>My name is Rahul<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2: Printing Age<\/strong><\/h3>\n\n\n\n<p>age = 24<br>print(f&#8221;I am {age} years old&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>I am 24 years old<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3: Combining Multiple Variables<\/strong><\/h3>\n\n\n\n<p>city = &#8220;Chennai&#8221;<br>profession = &#8220;Developer&#8221;<br>print(f&#8221;I live in {city} and work as a {profession}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>I live in Chennai and work as a Developer<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using Variables Inside f Strings<\/strong><\/h2>\n\n\n\n<p>One of the main benefits of f-strings is that they let you insert variables directly into strings.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>product = &#8220;Laptop&#8221;<br>price = 55000<br>print(f&#8221;The price of the {product} is Rs.{price}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>The price of the Laptop is Rs. 55000<\/p>\n\n\n\n<p>This makes creating dynamic <a href=\"https:\/\/www.guvi.in\/blog\/what-is-a-string-in-python\/\">strings<\/a> easier in real applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Performing Calculations in f Strings<\/strong><\/h2>\n\n\n\n<p>You can also perform calculations directly inside strings.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>&nbsp;a = 10<br>b = 20<br>print(f&#8221;The sum is {a + b}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>The sum is 30<\/p>\n\n\n\n<p>Another example:<\/p>\n\n\n\n<p>&nbsp;length = 5<br>width = 4<br>print(f&#8221;Area = {length * width}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Area = 20<\/p>\n\n\n\n<p>This feature cuts down on the need for extra variables.<\/p>\n\n\n\n<p>You can also download <strong>HCL GUVI\u2019s<\/strong> <a href=\"https:\/\/www.guvi.in\/mlp\/python-ebook?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=How+to+Use+Python+f-Strings+With+Examples+\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Python ebook<\/strong><\/a> to learn variables, loops, functions, OOP concepts, and beginner-friendly Python programs in one place.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Formatting Numbers Using f Strings<\/strong><\/h2>\n\n\n\n<p>f strings support formatting options for decimals, percentages, and alignment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Decimal Formatting<\/strong><\/h3>\n\n\n\n<p>pi = 3.141592<br>print(f&#8221;Value of pi is {pi:.2f}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>The value of pi is 3.14<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Percentage Formatting<\/strong><\/h3>\n\n\n\n<p>score = 0.95<br>print(f&#8221;Success rate: {score:.0%}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Success rate: 95%<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Comma Separator<\/strong><\/h3>\n\n\n\n<p>salary = 1500000<br>print(f&#8221;Salary: {salary:,}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Salary: 1,500,000<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using Functions in f-Strings<\/strong><\/h2>\n\n\n\n<p>You can even call <a href=\"https:\/\/www.guvi.in\/blog\/what-is-function-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">functions<\/a> inside f-strings.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>name = &#8220;python&#8221;<br>print(f&#8221;Uppercase: {name.upper()}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Uppercase: PYTHON<\/p>\n\n\n\n<p>Another example:<\/p>\n\n\n\n<p>text = &#8220;machine learning&#8221;<br>print(f&#8221;Length: {len(text)}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Length: 16<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Multi-Line Strings<\/strong><\/h2>\n\n\n\n<p>f strings can also work with multi-line text.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>name = &#8220;Harini&#8221;<br>course = &#8220;Python&#8221;<br>message = f&#8221;&#8221;<br>Student Name: {name}<br>Course Name: {course}<br>&#8220;&#8221;&#8221;<br>print(message)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Student Name: Harini<br>Course Name: Python<\/p>\n\n\n\n<p>This helps generate reports or structured outputs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Escape Characters in f Strings<\/strong><\/h2>\n\n\n\n<p>Sometimes you may need to use quotation marks within f-strings.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>name = &#8220;Harini&#8221;<br>print(f&#8217;Her name is &#8220;{name}&#8221;&#8216;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Her name is &#8220;Harini.&#8221;<\/p>\n\n\n\n<p>You can use single and double quotes strategically to avoid syntax errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Real World Applications of Python f-Strings<\/strong><\/h2>\n\n\n\n<p>Python f-strings are widely used in various areas of software development because they make dynamic text generation easier.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Web Development<\/strong><\/h3>\n\n\n\n<p>Developers use f-strings to create dynamic messages, logs, and user outputs in Flask and Django applications.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>username = &#8220;Harini&#8221;<br>print(f&#8221;Welcome back, {username}&#8221;)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Data Science<\/strong><\/h3>\n\n\n\n<p>Data analysts use f-strings when displaying calculated outputs, metrics, and formatted reports.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>accuracy = 96.5<br>print(f&#8221;Model Accuracy: {accuracy}%&#8221;)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Automation Scripts<\/strong><\/h3>\n\n\n\n<p>Automation scripts often use f-strings for file names, logs, and notifications.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>filename = &#8220;report&#8221;<br>print(f&#8221;{filename}.pdf generated successfully&#8221;)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Machine Learning Projects<\/strong><\/h3>\n\n\n\n<p>Machine learning engineers use f-strings for printing training results and evaluation metrics.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>epoch = 10<br>loss = 0.245<br>print(f&#8221;Epoch {epoch} completed with loss {loss}&#8221;)<\/p>\n\n\n\n<p>If you want to practice more Python concepts beyond f-strings, explore this<a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\"> Python Tutorial for Beginners<\/a> guide.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantages of Using f-Strings<\/strong><\/h2>\n\n\n\n<ol>\n<li>Easier to read and write<\/li>\n\n\n\n<li>Faster than older formatting methods<\/li>\n\n\n\n<li>Supports calculations directly<\/li>\n\n\n\n<li>Reduces unnecessary code<\/li>\n\n\n\n<li>Makes dynamic string creation simple<\/li>\n\n\n\n<li>Widely used in modern Python applications<\/li>\n<\/ol>\n\n\n\n<p>Because of these advantages, f strings are seen as the preferred string formatting method in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Mistakes Beginners Make<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Forgetting the Letter f<\/strong><\/h3>\n\n\n\n<p>Wrong:<\/p>\n\n\n\n<p>name = &#8220;Harini&#8221;<br>print(&#8220;Hello {name}&#8221;)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Hello {name}<\/p>\n\n\n\n<p>Correct:<\/p>\n\n\n\n<p>name = &#8220;Harini&#8221;<br>print(f&#8221;Hello {name}&#8221;)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using Invalid Variables<\/strong><\/h3>\n\n\n\n<p>Wrong:<\/p>\n\n\n\n<p>print(f&#8221;Age is {age}&#8221;)<\/p>\n\n\n\n<p>If the variable is not defined, Python will raise an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Mixing Quotes Incorrectly<\/strong><\/h3>\n\n\n\n<p>Wrong:<\/p>\n\n\n\n<p>print(f&#8221;Python&#8217;s version is {version}&#8221;)<\/p>\n\n\n\n<p>If quotes don&#8217;t match, syntax errors may happen.<\/p>\n\n\n\n<p>Curious about how these concepts work in real life? Join <strong>HCL GUVI\u2019s<\/strong> <a href=\"https:\/\/www.guvi.in\/courses\/programming\/python-zero-to-hero\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=How+to+Use+Python+f-Strings+With+Examples+\"><strong>Python<\/strong><\/a> course to build Python projects and learn automation, backend development, and data science fundamentals.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>f-strings are one of the most useful features introduced in modern Python. They simplify string formatting, making it quicker and easier to understand, especially for beginners.<\/p>\n\n\n\n<p>Whether you are creating automation scripts, web applications, machine learning projects, or data analysis programs, you will often use f-strings in Python.<\/p>\n\n\n\n<p>Learning them early enables you to write more professional and readable Python code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1780372212467\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. What does the f mean in Python f strings?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The letter f stands for formatted string literal. It allows you to insert variables and expressions directly into strings.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780372217494\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. Are f-strings available in all Python versions?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, f strings were introduced in Python 3.6.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780372225482\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. Are f-strings faster than format()?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, in most cases, f strings are faster and clearer than the format() method.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780372235430\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. Can we perform calculations inside f-strings?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, you can perform expressions and calculations directly within curly braces.<br \/>Example:<br \/>a = 5<br \/>b = 10<br \/>print(f&#8221;Result = {a + b}&#8221;)<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780372251142\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>5. Are f strings recommended for beginners?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, f strings are beginner-friendly because they are simple, readable, and widely used in real-world Python development.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python offers several ways to format strings, but f-strings are among the simplest and most readable methods. They let developers easily insert variables, expressions, and calculations directly into a string without complicating the code. If you are learning Python for data science, automation, backend development, or machine learning, knowing about f-strings is important because they [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":114978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717],"tags":[],"views":"32","authorinfo":{"name":"Vishalini Devarajan","url":"https:\/\/www.guvi.in\/blog\/author\/vishalini\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/06\/how-to-use-python-f-strings-with-examples-300x115.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/06\/how-to-use-python-f-strings-with-examples.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113791"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=113791"}],"version-history":[{"count":3,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113791\/revisions"}],"predecessor-version":[{"id":114979,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113791\/revisions\/114979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/114978"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=113791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=113791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=113791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}