<jupyter_start><jupyter_text>String Formatting* The oldest method involves placeholders using the modulo `%` character.* An improved technique uses the `.format()` string method.* The newest method, introduced with Python 3.6, uses formatted string literals, called *f-strings*.String formatting injects items into a string rather than chaining items together using commas or string concatenation.<jupyter_code>player = 'Thomas'
points = 33 
print('Last night, ' + player + ' scored ' + str(points) + ' points.')  # concatenation
print(f'Last night, {player} scored {points} points.') # string formatting<jupyter_output>Last night, Thomas scored 33 points.
Last night, Thomas scored 33 points.<jupyter_text>Part I: Formatting with placeholders<jupyter_code>#two methods %s and %r convert any python object to a string using two separate methods: str() and repr().
print('I once caught a fish %s.' %'this \tbig')
print('I once caught a fish %r.' %'this \tbig')
x, y = 'some', 'more'
print("I'm going to inject %s text here, and %s text here."%(x,y))
print('I wrote %s programs today.' %3.75)
print('I wrote %d programs today.' %3.75)   
print('Floating point numbers: %5.2f' %(13.144))
print('Floating point numbers: %1.0f' %(13.144))
print('Floating point numbers: %1.5f' %(13.144))
print('Floating point numbers: %10.2f' %(13.144)) 
#13, the minimum number of characters the string should contain. Note whitespace are padded before 13.<jupyter_output>Floating point numbers: 13.14
Floating point numbers: 13
Floating point numbers: 13.14400
Floating point numbers:      13.14<jupyter_text>Part II: Formatting with the `.format()` methodhttps://www.programiz.com/python-programming/methods/string/format There are many examples in this  Advantages of the .format() method over the %s placeholder method<jupyter_code>print('The {2} {1} {0}'.format('fox','brown','quick')) #Inserted objects can be called by index position

print('First Object: {a}, Second Object: {b}, Third Object: {c}'.format(a=1,b='Two',c=12.3)) #Can use key words

print('A %s saved is a %s earned.' %('penny','penny')) # resue inserted objects without duplication
# vs.
print('A {p} saved is a {p} earned.'.format(p='penny'))
print("hello {0}, your balance is {1:9.3f}".format("Adam",230.2346)) #Positional arguments
print("hello {name}, your balance is {blc:9.3f}".format(name = "Adam", blc=230.2346)) #keyword arguments
# number formatting 
print("The number is:{:d}".format(123))
print("The float number is:{:f}".format(123.4567898))
# octal, binary and hexadecimal format
print("bin: {0:b}, oct: {0:o}, hex: {0:x}".format(12))<jupyter_output>The number is:123
The float number is:123.456790
bin: 1100, oct: 14, hex: c<jupyter_text>Alignment, padding and precision with `.format()`By default, `.format()` aligns text to the left, numbers to the right. You can pass an optional `` to set a left, center or right alignment:  **Note the 0, 1, 2 below refer to the index of the words within format().**<jupyter_code>print('{0:8} | {1:9}'.format('Fruit', 'Quantity'))
print('{0:8} | {1:9}'.format('Apples', 3.))
print('{0:8} | {1:9}'.format('Oranges', 10))
print('{0:<8} | {1:^8} | {2:>8}'.format('Left','Center','Right'))
print('{0:<8} | {1:^8} | {2:>8}'.format(11,22,33))
#You can precede the alignment operator with a padding character
print('{0:=<8} | {1:-^8} | {2:.>8}'.format('Left','Center','Right'))
print('{0:=<8} | {1:-^8} | {2:.>8}'.format(11,22,33))
#Field widths and float precision are handled in a way similar to placeholders. The two below are equivalent.
print('This is my ten-character, two-decimal number:%10.2f' %13.579) 
print('This is my ten-character, two-decimal number:{0:10.2f}'.format(13.579))
#Note that there are 5 spaces following the colon, and 5 characters taken up by 13.58, for a total of ten characters.
# integer arguments
print("The number is:{:d}".format(123))

# float arguments
print("The float number is:{:f}".format(123.4567898))<jupyter_output>The number is:123
The float number is:123.456790<jupyter_text>Part III: Formatted String Literals (f-strings)Introduced in Python 3.6, f-strings offer several benefits over the older `.format()` string method described above. For one, you can<jupyter_code>name = 'Fred'
print(f"He said his name is {name}.") #no .format() is necessary
print(f"He said his name is {name!r}") #Pass `!r` to get the string representation:
num = 23.45678
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:{10}.{6}}")<jupyter_output>My 10 character, four decimal number is:   23.4568
My 10 character, four decimal number is:   23.4568<jupyter_text>Note that with f-strings, *precision* refers to the total number of digits, not just those following the decimal. This fits more closely with scientific notation and statistical analysis. Unfortunately, f-strings do not pad to the right of the decimal, even if precision allows it:<jupyter_code>num = 23.45
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:{10}.{6}}")<jupyter_output>My 10 character, four decimal number is:   23.4500
My 10 character, four decimal number is:     23.45<jupyter_text>If this becomes important, you can always use `.format()` method syntax inside an f-string:<jupyter_code>num = 23.45
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:10.4f}")<jupyter_output>My 10 character, four decimal number is:   23.4500
My 10 character, four decimal number is:   23.4500
StarcoderdataJupyterStructuredCleanDedup