åœåº§ã®çè§£ãšããŠã¯
ãããããã©ã£ã¡ã§ããããã§ããããŒã ã«äŸããšèšã£ãŠãè¯ãã§ãããã
-
str.format()ã¯å°å ¥åœå %-style ã眮ãæããæå³ããããŸããã - å®éã«
str.format()ã®æ¹ãæšå¥šãããŠããææãããããã§ãã - çŸæç¹ã§ã¯ãã©ã¡ããæšå¥šãšãããŠããããæç€ºçãªèŠå®ã¯ãªãããã«èŠåããããŸãã
- å°æ¥ã©ããªããã¯åãããŸããããçŽè¿ã§ %-style ãæ¶ãããšããè°è«ã«ã¯ãªã£ãŠããŸããã
- (远èš) æ°ããããŒãžã§ã³ã§ã¯ f-stringãšåŒã°ãããã©ãŒãããæ¹æ³ã
f'This is a {stuff}'ã䜿ããŸã
æ¥æ¬ã®æžç±ãã
Pythonæ¬ã®é ãã(倱瀌ïŒ)åèãPythonææ³è©³è§£ãã«ãããããŸã (p128)
Python3.2ãŸã§ã¯ãPythonãã¥ãŒããªã¢ã«ãªã©ã§ãå€ã%æŒç®åã«ããæåååã¯ããããåé€ãããŸãããšãããŠããŸããããPython3.3ã§ã¯ãã®èšè¿°ãåé€ãããŠããŸãã
åèã®ãã®èšè¿°ãããããŒããããªã®ããã ãšãããšã©ã£ã¡ã䜿ã£ãŠãéãšããããšããªãããã ãªããšçè§£ããŠãŸããã
ãããæ¬çš¿çè ã®ã¹ã¿ãŒãã©ã€ã³ã§ãã
å ¬åŒããã¥ã¡ã³ãã§ã¯ã§ã¯ã©ãèšèŒãããŠããã
å ¬åŒããã¥ã¡ã³ãã®é ããé ãŸã§èŠéããšçµè«ãå€ããå¯èœæ§ããããŸããããã®äžã§ç®ã«æ¢ãŸã£ãé¢çœãç¹ã以äžã«æžããŠãããŸãã
æåã®Noteéšåã®å€é·ãé¢çœãã®ã§ãã®éšåãå šææ²èŒããŸãã以äžã§ã¯ãç¹ã«æ³šç®ãããéšåã¯å€ªåã«ããŸãã
Note: The formatting operations described here are modelled on Câs printf() syntax. They only support formatting of certain builtin types. The use of a binary operator means that care may be needed in order to format tuples and dictionaries correctly. As the new String Formatting syntax is more flexible and handles tuples and dictionaries naturally, it is recommended for new code. However, there are no current plans to deprecate printf-style formatting.
ãæšå¥šãããŠããŸããåé€äºå®ã¯ãªãããšãªã£ãŠããŸããåæ²èãšã¯ã¡ãã£ãšæå³åããéããŸãããpatchã§å€ãã£ãã®ãããããŸããã
ãããã«ãããã€ã³ãã¯ãæšå¥šãããŠããããšç¢ºãã«æžãããŠããããšããããæ ¹æ ã«ãstr.format()ãæšå¥šãããŠããããšããããšãäž»åŒµåºæ¥ãææã¯ãã£ããšèšããŸãã
ã㊠3.3ç³»ã§ã
Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer str.format() interface helps avoid these errors, and also provides a generally more powerful, flexible and extensible approach to formatting text.
ãCèšèªã®printf()ã®ææ³ã«åºã¥ããã¿ãããªèª¬æããŸãã£ãšæ¶ããŠãŸããããã¯ãšãããã
ãæšå¥šãã®èšèãæ¶ããŠãŸãããã ãããã¯ãã«ã ãïŒããšãããŸããŠããŸãããã¯åŠå®ããã®ã¯é£ããããšæããŸããäºå®æ··ä¹±ãæãã»ã©ãã¯ãã«ã§ããã
3.6ã§ããçŸåšã®ææ°ã§ãã
Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text.
ãcommon errorsããšããã®ã¯äŸãã°ãããšãã§ããããããcommon errorsãåé¿ã§ãããããããšããããšãããæšå¥šãšã¯æžãããŠãªããã®ã® str.format() ã®æ¹ãè¯ãããã¿ãããªãã¥ã¢ã³ã¹ã¯äŒãã£ãŠããŸãã
ãŸã ãªãªãŒã¹ãããŠããªã3.7ç³»ãèŠãŠã¿ãŸãã
Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.
ãstr.format()æšå¥šããããããããã«è¯ãç¹ã»æªãç¹ãããããã¿ãããªè¡šèšãŸã§è¿œå ãããŸãããçè ç®ç·ã§ããšéäžã®æããäžæã§ãã
ãã®Noteã仿§ã®å šãŠãèšèŒããŠããªãããšã«ã¯æ³šæãå¿ èŠã§ããããã³ã·ã§ã³ã¯äŒãã£ãŠããæ°ã¯ããŸããéã«ãããä»ã®ç®æã§çµè«ãå€ããæãããã°æ¯éææé ãããã
å ã®PEPã§ã¯å ¥ãæ¿ããããšãæå³ããŠãã
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator.
Python 3.0ã®é ã¯ãã£ãšéå¿çã ã£ãã®ã§ãããããã ãprintf()ãšã®ã¢ãããžãŒã®äººæ°ã匷ãããšãããã®ãreplaceããšããè°è«èªäœãçž®éããŠããå°è±¡ã§ãã
æžç±ã§ã¯ã©ãã
ãpython format recommendedããªã©ãšã°ã°ããšGoogle Booksçµç±ã§ãMastering Pythonããšããæžç±ã®ä»¥äžã®èšè¿°ãç®ã«çãŸããŸããã
In general most people
str.formatthese days, but it mainly comes down to preference. Theprintf-styleis simpler, while thestr.formatmethod is more powerful.
ã¡ãªã¿ã«åèã®èè ã¯printf()ã®æµããã%-styleã奜ããªããã§ãã
æ¬çš¿çè ã¯ã©ãæã£ãŠããã
èªåã§ã¯ååãšã㊠str.format() ãã䜿çšããŠããŸãããäººã«æããéã« %-style ãçç¥ãããšããããšããã°ãã°ã§ãã
ãã ãããã¯ãæšå¥šãããŠããããããšããæå³åãããã¯ãæŒ ç¶ãšãã奜ã¿ã®åé¡ããªãšæããŸãã%-styleã䜿ããããšãŠããšããã®ããŸãåæã«äºå®ã§ãããä»äººã«ã䜿ããªããšèšãã«ã¯ãç§ãšããŠã¯èª¬åŸææãè¶³ããªãå°è±¡ãæã¡ãŸãã
ããŒã å
ã§ã®äžè²«æ§ãæ±ããããã«çµ±äžãããªã str.format() ãåŒ·ãæšããŸãããã ãä»®ã«ã©ã£ã¡ã§ãè¯ããšããã³ãŒãã£ã³ã°ã¹ã¿ã€ã«ã§ããã°ããã«åŸã£ãŠåæ¹æ±ããããã«ããŠããã®ãçãšèããŸãããã©èŠãããšãããGoogleã®Style Guideã§ããšãäž¡æ¹OKããšãªã£ãŠããŸãã (Revision 2.59)ã
ããã§èšãããã®ã¯ãPythonå
¬åŒã®ããã¥ã¡ã³ããæ ¹æ ã«ããéãã%-styleã䜿ã£ãŠãã人㫠str.format() 䜿ããããšããå§åããããçåãã¯ããŸããªãããã ããšããããšã§ããæ°ãããã®å¥œããªã®ã¯åãããã str.format() ã®æ¹ããã¯ãã«ãªã®ãçè§£åºæ¥ãŸããããã以äžã®ãäžããç®ç·ãã %-style ã«å¯ŸããŠããæçŸ©ã¯ãªããªããšã
äžæ¹ str.format() ãç¥ããšã©ãããŠãããããç®ç·ã§ %-style ãèŠãŠããŸãã®ã§ããéå»ã®è°è«ãããå
¬åŒã«æšå¥šãããå
¥ãæ¿ãäºå®ã«ãªã£ãŠãããäºå®ãäœèšã«ããããç®ç·ãä»å ããã®ããªãããªããŠæã£ããããŸãã
åæ²èãMastering Pythonãã¯é¢çœã衚çŸã§ãéããŠãããæ°ã¯ããŸããPythonå ¬åŒãšããŠã®æšå¥šã§ã¯ãªããã©å€ãã®äººãæšå¥šããŠããããšãããšããããã®äºã€ã®å·®ã¯å€§ãããããããŸããããPythonã®éçºå ãå ¬åŒã«æšå¥šããŠãããå°æ¥çã«%-styreãåé€ãããããšããæå³ã«ãããæšå¥šãªãæ¯éããªãã%-style ã¯äœ¿ãã¹ãã§ãªãããšããè°è«ã«ãªãããã§ãããä»ã¯ããããæµãã«ã¯ãªã£ãŠãªãããããšããã®ãæ¬çš¿ã®åœåº§ã®çµè«ã§ãã
æŒ ç¶ãšãstr.format()ãæšå¥šãããŠããããšãã衚çŸãè³ã«ããŸããããããäºå®ãšããã«ã¯å°ã
æªããïŒçŸæç¹ã§ã¯æç¢ºãªæ ¹æ ããªãïŒãšèããŸããå°æ¥ã¯äžæã§ãããä»èšèªãšã®æ¥ç¹ãšããé¢ããã£ãŠã %-style ãå»ããã®ã¯ã¡ãã£ãšèãã¥ããæã¯ãããŸãã
ããã«ãçè ã仿§å šæãèªãã§ããããã§ã¯ãªãããšãéçºè èªèº«ã§ããªãããšããããã®èŸºãã«ã€ããŠãã®æç« ãçµè«ãåºããããšã¯ãããŸãããæ°æ å ±ãåŸ ã¡ããŠãããŸãã
ãã®ä»¶ã«ã€ããŠããããã©ãã§ãè¯ãããšããæèŠã«ã¯åæããŸããã§ãæ¬æãæåŸãŸã§èªãã ã®âŠâŠïŒ :-)
ãã®ä»ã®æ å ±æº
- https://pyformat.info/ ... äž¡æ¹ã®ãã©ãŒãããã§ã®èšèŒæ¹æ³ã䞊ã¹ãããŠããç§éžã§ã
è¿œèš (2017-08-31)
- 2016幎ãã©ã£ãã®PyCon JPã§ %-style ãåé€ããäºå®ã¯ããªãããšãã話ããã£ããšããªãã£ããšããæ å ±ãåŸ ã¡ããŠãããŸãã
- Python 3.7 ã§å°å
¥ããã f-strings ãšã®é¢ä¿ã«èå³ãæã€æ¹ãè€æ°ããã£ããã£ãã®ã§ããïŒã³ã¡ã³ãçããããšãããããŸãïŒã %-style ãš
str.format()ã®é¢ä¿ã«ã€ããŠã®è°è«ãæ·±ãŸãããã§ããªãããšãªã©ããæ¬çš¿ã§ã¯æ±ã£ãŠããŸãããäœããã®æèŠã衚æããã«ã¯å®è·µãããŒã¹ã«ããçµéšè«çãæå ã«ãããŸããã
è¿œèš (2019-04-09)
- f-string ãšã®å¯Ÿæ¯ã®è°è«ã¯æ¬èšäºã®ãã©ãŒã«ã¹ã§ã¯ãªãã§ããæµç³ã«ããããåé ã§èšåããã¹ãææããªãšæã£ãã®ã§è¿œèšããŸããã