3.2. String Formatting#

The fastest and latest way to do string formatting is using the F-strings.

  PI = 3.14159265359 
  print(f'{PI:.2f}')
3.14

But you have to know the other ways so you can read older code or use these ways if you have to use an older version of Python

3.2.1. What can you format?#

String-formatting allows you to do at least five things. These string-format specifiers come after a colon.

  1. specify width

  2. align data to left, right, or center; make sure to explain the default alignment for string and number; make sure to

  3. specify padding character when specified width is greater than the width of the string/number being used

  4. specify precision for floating values

  5. add commas to numbers for easier viewing;

3.2.2. Oldest Method#

  PI = 3.14159265359 
  name = 'PI'
  print('%s is %.2f' % (name, PI))  # oldest way format specifier is <width>.<precision><type>
PI is 3.14

3.2.3. Newer Method#

Still used for creating string templates.

PI = 3.14159265359 
name = 'PI'
print(('{0} is {1:.2f}'.format('PI', PI)) ) # 
PI is 3.14

3.2.4. Newest Method#

Newest and fastest method for string formatting. {<variable or expression>:<format-specifier>} where the format specifier is: <padding_character><alignment><width>.<comma><precision><type>

  • padding_character can be anything. most common are

    • space (default)

    • -

    • 0

  • alignment

    • < – left aligned

    • ^ – center aligned

    • > – right aligned

  • width is a number. If the width is greater than the number of characters, then:

    • strings are left aligned

    • numbers are right aligned

  • comma is used to add commas to large numbers for easier viewing

  • precision controls how many decimal places to show. NOTE This uses round, but leaves trailing zeros.

  • type specifies the data type

    • f – float

    • d – integer

    • s – string

PI = 3.14159265359 
name = 'PI'
# {<variable or expression>:<format-specifier>} where the format specifier is <width>.<precision><type>
print(f'{name} is {PI:.2f}') # newest way
PI is 3.14

Reference: https://pyformat.info/

a_str = 'EAS503'
an_int = 113
a_float = 92.3
print(a_str, an_int, a_float)
EAS503 113 92.3
line1 = 'This is the first line.\n'
line2 = 'This is the second line.'
lines = line1 + line2
line = 'This is the first line.\n'
line += 'This is the second line.'

3.2.5. Example 1#

course_number = 'EAS503'
class_size = 113
class_average = 92.3

str_format = '{}'.format(course_number)
f_string = f'{course_number}'

print(str_format)
print(f_string)
EAS503
EAS503

3.2.6. Example 2#

course_number = 'EAS503'
class_size = 113
class_average = 92.3

str_format = 'The course number is {}.'.format(course_number)
f_string = f'The course number is {course_number}.'

print(str_format)
print(f_string)
The course number is EAS503.
The course number is EAS503.

3.2.7. Example 3 use index#

course_number = 'EAS503'
class_size = 113
class_average = 92.3
str_format = 'The course number is {}. It has {} students.'.format(course_number, class_size)
str_format = 'The course number is {0}. It has {1} students.'.format(course_number, class_size)
f_string = f'The course number is {course_number}. It has {class_size} students.'

print(str_format)
print(f_string)
The course number is EAS503. It has 113 students.
The course number is EAS503. It has 113 students.

3.2.8. Example 4 change index#

course_number = 'EAS503'
class_size = 113
class_average = 92.3
str_format = 'The course number is {1}. It has {0} students.'.format(course_number, class_size)
f_string = f'The course number is {class_size}. It has {course_number} students.'

print(str_format)
print(f_string)
The course number is 113. It has EAS503 students.
The course number is 113. It has EAS503 students.

3.2.9. Example 5 adding a float#

course_number = 'EAS503'
class_size = 113
class_average = 92.3
str_format = 'The course number is {0}. It has {1} students. The class average is {2}.'.format(course_number, class_size, class_average)
f_string = f'The course number is {class_size}. It has {course_number} students. The class average is {class_average}.'

print(str_format)
print(f_string)
The course number is EAS503. It has 113 students. The class average is 92.3.
The course number is 113. It has EAS503 students. The class average is 92.3.

3.2.10. Example 6 specify number of spaces to use – width#

course_number = 'EAS503'
class_size = 113
class_average = 92.3
str_format = 'The course number is {0:10}. It has {1:10} students. The class average is {2:10}.'.format(course_number, class_size, class_average)
f_string = f'The course number is {course_number:10}. It has {class_size:10} students. The class average is {class_average:10}.'

print(str_format)
print(f_string)
The course number is EAS503    . It has        113 students. The class average is       92.3.
The course number is EAS503    . It has        113 students. The class average is       92.3.

3.2.11. Example 7 right align#

course_number = 'EAS503'
class_size = 113
class_average = 92.3
str_format = 'The course number is {0:>10}. It has {1:>10} students. The class average is {2:>10}.'.format(course_number, class_size, class_average)
f_string = f'The course number is {course_number:>10}. It has {class_size:>10} students. The class average is {class_average:>10}.'

print(str_format)
print(f_string)
The course number is     EAS503. It has        113 students. The class average is       92.3.
The course number is     EAS503. It has        113 students. The class average is       92.3.

3.2.12. Example 8 left align#

course_number = 'EAS503'
class_size = 113
class_average = 92.3
str_format = 'The course number is {0:<10}. It has {1:<10} students. The class average is {2:<10}.'.format(course_number, class_size, class_average)
f_string = f'The course number is {course_number:<10}. It has {class_size:<10} students. The class average is {class_average:<10}.'

print(str_format)
print(f_string)
The course number is EAS503    . It has 113        students. The class average is 92.3      .
The course number is EAS503    . It has 113        students. The class average is 92.3      .

3.2.13. Example 9 center align#

course_number = 'EAS503'
class_size = 113
class_average = 92.3
str_format = 'The course number is {0:^10}. It has {1:^10} students. The class average is {2:^10}.'.format(course_number, class_size, class_average)
f_string = f'The course number is {course_number:^10}. It has {class_size:^10} students. The class average is {class_average:^10}.'

print(str_format)
print(f_string)
The course number is   EAS503  . It has    113     students. The class average is    92.3   .
The course number is   EAS503  . It has    113     students. The class average is    92.3   .

3.2.14. Example 10 Padding with zeros#

  • Zero padding does not require a alignment specifier

student_id = 223333

str_format = 'The number padded {} padded with zeros {:08}'.format(student_id, student_id)
f_string = f'The number padded {student_id} padded with zeros {student_id:08}'

print(str_format)
print(f_string)
The number padded 223333 padded with zeros 00223333
The number padded 223333 padded with zeros 00223333

3.2.15. Example 11 Padding with dashes#

student_id = 223333

str_format = 'The number padded {} padded with zeros {:->8}'.format(student_id, student_id)
f_string = f'The number padded {student_id} padded with zeros {student_id:->8}'

print(str_format)
print(f_string)
The number padded 223333 padded with zeros --223333
The number padded 223333 padded with zeros --223333

3.2.16. Example 12 Adding Commas#

number = 123456
f'{number:,}'
'123,456'
number = 123456.2345
f'{number:,.2f}'
'123,456.23'

3.2.17. Rounding#

  • When you use .2f, for example, the f-string will round, but keep the trailing zero. When you use the round() function, the trailing zero is dropped. Study the following examples.

f'{3.5:.0f}'
'4'
f'{4.5:.0f}'
'4'
f'{2.675:.2f}'
'2.67'
f'{4.5:.2f}'
'4.50'
round(4.5, 2)
4.5
f'{4.5:.0f}'
'4'
f'{4.8999:.2f}'
'4.90'
round(4.8999, 2)
4.9