How to create a String with format?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a formatted string means building text from values such as names, numbers, dates, or computed expressions. The exact syntax depends on the language, but the underlying goal is the same: insert dynamic values into a readable template without manual concatenation everywhere. Good formatting code is easier to read, less error-prone, and usually easier to localize later.
Why Formatting Is Better Than Manual Concatenation
You can always build strings with +, but that becomes messy once formatting rules matter.
For example, this style gets awkward quickly:
A formatting API makes the structure clearer and handles type conversion more naturally.
Python Examples
Modern Python usually uses f-strings.
Output:
You can also use str.format() if you need a reusable template.
F-strings are usually the most readable when the values are already in local variables.
Java Examples
In Java, the standard option is String.format().
The placeholders use format specifiers:
- '
%sfor strings' - '
%dfor integers' - '
%ffor floating-point numbers'
So %.1f means "show one digit after the decimal point."
JavaScript Examples
JavaScript does not have a direct equivalent to String.format() in the language core, but template literals are the standard solution.
Template literals are also convenient for multi-line text.
Formatting Rules Matter Too
Formatting is not only about inserting values. It is also about how those values should look.
Examples include:
- limiting decimal places
- left or right alignment
- zero padding
- date or currency formatting
A simple Python example with alignment:
A comparable Java example:
Once you start formatting tables, logs, or reports, these details matter a lot.
Reusable Templates Are Often Better
If the same text pattern appears in several places, keep the format in one template instead of rebuilding it repeatedly. That makes later edits easier and reduces subtle formatting drift across the codebase.
This matters especially for logs, CLI output, and user-facing messages where consistency is part of maintainability.
Common Pitfalls
- Using manual concatenation for complex templates and making the code hard to read.
- Forgetting numeric format specifiers such as decimal precision.
- Mixing placeholder styles incorrectly, especially when switching between languages.
- Building user-facing messages inline everywhere instead of keeping reusable templates.
- Assuming formatting automatically handles localization for dates, numbers, or currency.
Summary
- Formatted strings are cleaner than repeated concatenation.
- Python commonly uses f-strings or
str.format(). - Java commonly uses
String.format()with format specifiers. - JavaScript commonly uses template literals.
- Good string formatting is about both inserting values and controlling how those values are displayed.
Related reading
- How to create abstract properties in python abstract classes?
- How to create an async generator in Python?
- How to create an ec2 instance using boto3
- How to create conda environment with specific python version?
- How to create a sub array from another array in Java?
- How to create a temporary directory/folder in Java?
- How to Create Dataframe from AWS Athena using Boto3 get_query_results method
- How to create dict from class without None fields?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.