[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)

7.1.2 Formatting

7.1.2.1 Style with%

--Old style formatting uses the format string% data. You can specify a width limit, a character limit, placement, and padding between the% and the type specifier.


#String
>>> "%s" % 42
`42`
#Decimal integer
>>> "%d" % 42
`42`
#Hexadecimal integer
>>> "%x" % 42
`2a`
#Octal integer
>>> "%o" % 42
`52`

#Decimal float
>>> "%f" % 7.03
`7.030000`
#Exponential form float
>>> "%e" % 7.03
`7.030000e+00`
#Depends on the size of the digit%g (Decimal float or exponential float)
>>> "%g" % 7.03
`7.03`
#Integer and literal%
>>> "%d%%" % 100
`100%`

>>> actor = "Richard Gere"
>>> cat="Chester"
>>> weight=28
#Insert string
>>> "My wife is favorite actor is %s" % actor
`My wife is favorite actor is Richard Gere`
#When inserting multiple character strings(cat,weight)Must be tupled like this.
>>> "Our cat %s weights %s pounds" % (cat,weight)
'Our cat Chester weights 28 pounds'

>>> n = 42
>>> f=7.03
>>> s="string cheese"
#Displayed with default width.
>>> "%s %f %s" %(n,f,s)
'42 7.030000 string cheese'
#A minimum width of 10 was set for each variable and right justified.
>>> "%10s %10f %10s" %(n,f,s)
'        42   7.030000 string cheese'
#Left-aligned using the same width.
>>> "%-10s %-10f %-10s" %(n,f,s)
'42         7.030000   string cheese'
#The width of the field is the same, and the upper limit of the number of characters is set to 4 and right-aligned. If you do this, part of the character string will be truncated, and the number after the decimal point will be 4 digits.
>>> "%10.4d %10.4f %10.4s" %(n,f,s)
'      0042     7.0300       stri'
#Limit the number of characters without specifying the lower limit of the field width.
>>> "%.4d %.4f %.4s" %(n,f,s)
'0042 7.0300 stri'
#Field width and number of characters*Argument by
>>> "%*.*d %*.*f %*.*s" %(10,4,n,10,4,f,10,4,s)
'      0042     7.0300       stri'

7.1.2.2 New style with {} and formatting


>>> "{} {} {}".format(n,f,s)
`42 7.03 string cheese`

#{}The number in indicates the index in format.
>>> "{2} {0} {1}".format(n,f,s)
`string cheese 42 7.03`

#The argument may be a dictionary or keyword argument. And it is also possible to put the key and name in the format specification.
>>> "{n} {f} {s}".format(n=42,f=7.03,s="string cheese")
`42 7.03 string cheese`


>>> d={"n":42,"f":7.03,"s":"string cheese"}
#{0}Is format()Refers to the dictionary called argument d in.
#{2}Is format()Refers to the character string other in the argument.
>>> "{0[n]} {0[f]} {0[s]} {1}".format(d,"other")
`42 7.03 string cheese other`

#%Instead of:Can be used.
>>> "{0:d} {1:f} {2:s}".format(n,f,s)
`42 7.030000 string cheese`
#Specified by keyword argument
>>> "{n:d} {f:f} {s:s}".format(n=42,f=7.03,s="string cheese")
`42 7.030000 string cheese`
#The default right justification with the lower limit of the field width set to 10.
>>> "{0:10d} {1:10f} {2:10s}".format(n,f,s)
`        42   7.030000 string cheese`
#Same right alignment as above, but>It is easier to understand by using.
>>> "{0:>10d} {1:>10f} {2:>10s}".format(n,f,s)
`        42   7.030000 string cheese`
#Left justified
>>> "{0:<10d} {1:<10f} {2:<10s}".format(n,f,s)
`42         7.030000   string cheese`

#Centered
>>> "{0:^10d} {1:^10f} {2:^10s}".format(n,f,s)
`    42      7.030000  string cheese`

#Unlike the old style, the precision specified after the decimal point specifies the number of digits after the decimal point for float and the upper limit for the number of characters for character strings, but it can no longer be used with integers.
>>> "{0:>10.4d} {1:>10.4f} {2:>10.4s}".format(n,f,s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Precision not allowed in integer format specifier
>>> "{0:>10d} {1:>10.4f} {2:>10.4s}".format(n,f,s)
`        42     7.0300       stri`

#:By specifying immediately after, and before specifying the alignment and width, the gaps in the output field can be filled with characters other than spaces.
>>> "{0:!^20s}".format("BIG SALE")
`!!!!!!BIG SALE!!!!!!`

Impressions

The update frequency has become vacant recently. Posting was a little sloppy. .. ..

I'm studying, but I'm having a hard time with the database in Chapter 8. The story of the database is abstract and hard to come to my mind, but it seemed like I was getting an error even though I was proceeding according to the main story. Tomorrow I will take time to do Chapter 8 again. The continuation of Chapter 7 will be posted tomorrow.

References

"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"

"Python Tutorial 3.8.1 Documentation 7. Input and Output" https://docs.python.org/ja/3/tutorial/inputoutput.html#old-string-formatting

Recommended Posts

[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
[Introduction to Python3 Day 4] Chapter 2 Py Components: Numbers, Strings, Variables (2.3.7-2.4)
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
[Introduction to Python3 Day 8] Chapter 4 Py Skin: Code Structure (4.1-4.13)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Udemy Python3 + Application] 11. Character strings
Introduction to Effectiveness Verification Chapter 1 in Python
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 10] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.4-5.7)
[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)
[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Python day 1
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Python Tutorial] An Easy Introduction to Python
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 1
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 2
[Introduction to Udemy Python3 + Application] 63. Generator comprehension