Site icon Bernard Aybout's Blog – MiltonMarketing.com

Python String Formatting

Python String Formatting

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.

Exit mobile version