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.
Python Quick Tip: F-Strings – How to Use Them and Advanced String Formatting
Related Videos:
Related Posts:
Learn about Python Sets (lists)
Learn Coding with Milton Marketing Top 20 Programming Languages
Introduction to JavaScript – Variables: Mathematical Assignment Operators
Your first Hello World app in C# Sharp
Enhance WordPress Formatting with TinyMCE Editor Plugin
Learn more about JavaScript Operators
Learn RE – Regular Expressions in Python
Comparing Python to other languages
Exception Handling in Python programming
Learn about Dictionaries datatype for Python
Learn about Numpy Arrays in Python programming
Learn Modules and Packages in Python programming
Learn about programming Loops in Python
How can Alice help teach OOP (Object Oriented Programming)?
Who is this Android App Development course for?
Python 3 Object Oriented Programming
Google is about to have a lot more ads on phones
Python 3 Object-Oriented Programming – free eBook download
Python Online Compiler Code on the Go
Learn Code Introspection Python Programming
Russian hackers are eight times faster than North Korean groups
Introduction to JavaScript – Built-in Methods
GitHub’s and more best FREE guides for Python developers
Hackers Have Just Put 620 Million Accounts Up For Sale On The Dark Web — Are You On The List?
B0r0nt0K Ransomware Wants $75,000 Ransom, Infects Linux Servers