python|May 14, 2019|2 min read

Python 3 - Format String fun

TL;DR

Use Python 3 f-strings (format strings) by prefixing a string with 'f' and placing variables inside curly braces for clean and readable string interpolation.

Python 3 - Format String fun

This post is dedicated for cases where we intend to append a variable value in a string, whether in logs, string message etc.

Use Format String

Just put a character f before your string, and put all variable in between curly braces.

Example

website_name = 'GyanByte'
year = 2019

print (f'This website: {website_name} has written this post in year: {year}')

>>> This website: GyanByte has written this post in year: 2019

This is fantastic way of using string formatting, and is quite easily readable too.

By Placeholders

Lets look at above example

website_name = 'GyanByte'
year = 2019

print ('This website: %s has written this post in year: %d' %(website_name, year))

# Note: %s for string, and %d for integers

Positional Formatting

Its kind of same as above. But, you use curly braces instead of percentage format.

website_name = 'GyanByte'
year = 2019

print ('This website: {} has written this post in year: {}'.format(website_name, year))

You can use numbers in curly braces if you want to specify which variables comes when:

print ('This website: {0} has written this post in year: {1}. Thanks from {0}'.format(website_name, year))
>>> This website: GyanByte has written this post in year: 2019. Thanks from GyanByte

print ('This website: {1} has written this post in year: {0}'.format(website_name, year))
>>> This website: 2019 has written this post in year: GyanByte

#Note: the position number in curly braces

But, you can not mix them!

print ('This website: {1} has written this post in year: {}'.format(website_name, year))

# error: ValueError: cannot switch from manual field specification to automatic field numbering

Simple Concatenation

You can concatenate strings simply by + operator.

s = 'hi' + ' ' + 'GyanByte'
print(s)

>>> 'hi GyanByte'

This is a simple way, but doesn’t look good.

Related Posts

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Python 3 - Magical Methods and Tricks with extremely useful methods

Python 3 - Magical Methods and Tricks with extremely useful methods

This post will show some really awesome tricks in python. Get the power of a…

Python 3 - Fun with Python String

Python 3 - Fun with Python String

This post some useful tips of using strings, and some issues while dealing with…

Understanding and Solving pylint errors for Python 3

Understanding and Solving pylint errors for Python 3

Pylint is an excellent tool to have good quality code. This post is not about…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…