Bernard Aybouts - Blog - Miltonmarketing.com

Approx. read time: 3.6 min.

Post: Python String Formatting

Python String Formatting

In Python, you can format strings using C-style string formatting to create new, formatted strings. The “%” operator is used for this purpose. You can use it to format a set of variables, enclosed in a tuple (a fixed-size list), with a format string that contains normal text and “argument specifiers” like %s and %d.

Single Variable Formatting

Let’s start with a simple example where we print a greeting to a user. Suppose you have a variable called name with your username in it.

# This prints out "Hello, John!"
name = "John"
print("Hello, %s!" % name)
    

Assignment 1:

Create a variable named username with your name and print out “Welcome, [your name]!”

# Write your code here
username = "YourName"
print("Welcome, %s!" % username)
    

Answer for Assignment 1:

username = "YourName"
print("Welcome, %s!" % username)
    

Multiple Variable Formatting

To use two or more argument specifiers, use a tuple (enclosed in parentheses).

# This prints out "John is 23 years old."
name = "John"
age = 23
print("%s is %d years old." % (name, age))
    

Assignment 2:

Create variables first_name and years with your first name and age respectively, and print out “[Your name] is [your age] years old.”

# Write your code here
first_name = "YourName"
years = YourAge
print("%s is %d years old." % (first_name, years))
    

Answer for Assignment 2:

first_name = "YourName"
years = YourAge
print("%s is %d years old." % (first_name, years))
    

Formatting Different Types

Any object which is not a string can be formatted using the %s operator. The repr method of the object is used to convert it to a string.

# This prints out: A list: [1, 2, 3]
mylist = [1, 2, 3]
print("A list: %s" % mylist)
    

Assignment 3:

Create a variable my_numbers with a list of numbers [4, 5, 6] and print “Numbers: [4, 5, 6]”.

# Write your code here
my_numbers = [4, 5, 6]
print("Numbers: %s" % my_numbers)
    

Answer for Assignment 3:

my_numbers = [4, 5, 6]
print("Numbers: %s" % my_numbers)
    

Basic Argument Specifiers

Here are some basic argument specifiers you should know:

  • %s – String (or any object with a string representation, like numbers)
  • %d – Integers
  • %f – Floating point numbers
  • %.2f – Floating point numbers with a fixed number of digits after the decimal point (2 digits in this case)
  • %x/%X – Integers in hexadecimal representation (lowercase/uppercase)

Comprehensive Example

You will need to write a format string that prints out the data using the following syntax: “Hello John Doe. Your current balance is $53.44.”

# This is the solution.
data = ("John", "Doe", 53.44)
format_string = "Hello %s %s. Your current balance is $%.2f."

print(format_string % data)
    

Assignment 4:

Create a tuple account_info with the values (“Jane”, “Smith”, 150.75) and print “Hello Jane Smith. Your current balance is $150.75.”

# Write your code here
account_info = ("Jane", "Smith", 150.75)
format_string = "Hello %s %s. Your current balance is $%.2f."
print(format_string % account_info)
    

Answer for Assignment 4:

account_info = ("Jane", "Smith", 150.75)
format_string = "Hello %s %s. Your current balance is $%.2f."
print(format_string % account_info)
    

Now that you have practiced basic string formatting in Python, try creating your own formatted strings with different variables and types.

For privacy reasons YouTube needs your permission to be loaded. For more details, please see our Privacy Policy – Legal Disclaimer – Site Content Policy.

The Longevity Blueprint: AI-Powered Health Optimization

Current step:1AI-Human Medical Analyzer: Smarter, Personalized Health
2AI-Human Medical Analyzer: Smarter, Personalized Health

> SYS.HEALTH: AI-Human Medical Analyzer_

// Revolutionize Your Diagnostics

Experience the perfect blend of cutting-edge AI precision and expert human care. Our revolutionary analyzer turns your raw health data into personalized, actionable insights tailored just for you.

> INITIALIZING_BIOMETRIC_SCAN...

[+] DATA_INPUT

Securely upload complex health parameters, including lab bloodwork and comprehensive medical history.

[+] PROCESSING

Advanced algorithmic parsing combined with human-level oversight ensures hyper-accurate data interpretation.

[+] OUTPUT_MATRIX

Receive smarter, faster, and truly personalized care strategies to take immediate charge of your health journey.

A name/nickname is required to continue.

> TRANSLATION_MATRIX_ACTIVE...
[ LANG_EN ]
Knowledge Heals, Prevention Protects
[ LANG_HI ]
ज्ञान ठीक करता है, रोकथाम सुरक्षा करती है
[ LANG_ZH ]
知识治愈,预防保护
[ LANG_JA ]
知識は癒し、予防は守る
[ LANG_HE ]
הידע מרפא, המניעה מגנה
[ LANG_AR ]
المعرفة تُشفي، والوقاية تحمي
[ LANG_FR ]
La connaissance guérit, la prévention protège

> SYS.AUTH: Data Processing Consent_

[ AWAITING_AUTHORIZATION ] By providing consent, you allow us to process your uploaded data through our proprietary AI-Human analysis system.

  • [+] SECURE_REVIEW: This ensures your information is carefully reviewed using advanced AI technology and certified professional oversight to deliver personalized health insights.
  • [+] PRIVACY_LOCK: Your privacy is our strict priority. Your data will only be used for this specific diagnostic purpose.

> SYS.UPLOAD: Share Medical Records [OPTIONAL]_

[ USER_CONTROL_ACTIVE ] Uploading your medical records during registration is entirely optional. You can choose to bypass this step and provide data later if it suits your timeline.

You dictate the data flow: share as much or as little as you’re comfortable with, and let us guide you toward better health.

[+] FORMAT_SUPPORT

We accept all file formats, including photos, PDFs, text documents, and raw official medical data.

[+] DATA_YIELD

Increased inputs correlate with higher precision. The more info you share, the better we tailor your personalized insights.

> NEXT_STEPS: Post-Registration Protocol_

Once your registration is complete, a human specialist from our team will personally reach out to you within 3-10 business days. We will discuss your health journey and map out exactly how we can support you.

About the Author: Bernard Aybout (Virii8)

Avatar Of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀