{"id":422,"date":"2017-02-28T18:06:17","date_gmt":"2017-02-28T09:06:17","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=422"},"modified":"2021-10-01T00:17:55","modified_gmt":"2021-09-30T15:17:55","slug":"%e6%96%87%e5%ad%97%e5%88%97%e3%81%ae%e3%83%95%e3%82%a9%e3%83%bc%e3%83%9e%e3%83%83%e3%83%88%ef%bc%88%e5%80%a4%e3%81%ae%e5%9f%8b%e3%82%81%e8%be%bc%e3%81%bf%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/422","title":{"rendered":"\u6587\u5b57\u5217\u3078\u306e\u57cb\u3081\u8fbc\u307f"},"content":{"rendered":"<p>\u3053\u306e\u30da\u30fc\u30b8\u3067\u306fPython\u306e\u6587\u5b57\u5217\u3078\u306e\u5909\u6570\u57cb\u3081\u8fbc\u307f\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<h2>\u6587\u5b57\u5217\u3078\u306e\u5909\u6570\u57cb\u3081\u8fbc\u307f<\/h2>\n<p>Python\u3067\u6587\u5b57\u5217\u306b\u5bfe\u3057\u3066\u5024\u3092\u57cb\u3081\u8fbc\u3080\u65b9\u6cd5\u306f\u3044\u304f\u3064\u304b\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u30da\u30fc\u30b8\u3067\u306f\u4ee5\u4e0b3\u3064\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>f\u6587\u5b57\u5217<\/li>\n<li>format\u30e1\u30bd\u30c3\u30c9<\/li>\n<li>printf \u5f62\u5f0f<\/li>\n<\/ul>\n<h2>f\u6587\u5b57\u5217<\/h2>\n<p>Python3.6\u4ee5\u964d\u3001\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u6e08\u307f\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb\u3068\u547c\u3070\u308c\u308b\u8a18\u6cd5\u304c\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002f\u6587\u5b57\u5217\u3068\u547c\u3070\u308c\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u3002\u30ea\u30c6\u30e9\u30eb\u306e\u5148\u982d\u306bf\u3092\u8a18\u8ff0\u3057\u3001\u57cb\u3081\u8fbc\u307f\u305f\u3044\u5909\u6570\u3092\u4e2d\u62ec\u5f27\u3067\u304f\u304f\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u5909\u6570\u306f\u4e88\u3081\u5b9a\u7fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nitem = &quot;apple&quot;\r\ntext = f&quot;There is an {item}.&quot;\r\nprint(text)  # There is an apple.\r\n\r\n<\/pre>\n<h2>format\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>\u6587\u5b57\u5217\u306eformat\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068\u4e2d\u62ec\u5f27\u3067\u7f6e\u63db\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u8a2d\u5b9a\u65b9\u6cd5\u306f3\u3064\u3042\u308a\u3001\u4e2d\u62ec\u5f27\u5358\u4f53\u3001\u4e2d\u62ec\u5f27\u5185\u306b\u9806\u5e8f or \u30ad\u30fc\u3092\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nitem1 = &quot;apple&quot;\r\nitem2 = &quot;banana&quot;\r\n\r\n# \u4e2d\u62ec\u5f27\u5358\u4f53\r\ntext1 = &quot;There are {} and {}.&quot;\r\nprint(text1.format(item1, item2))\r\n\r\n# \u9806\u5e8f\u6307\u5b9a\r\ntext2 = &quot;There are {0} and {1}.&quot;\r\nprint(text2.format(item1, item2))\r\n\r\n# \u30ad\u30fc\u6307\u5b9a\r\ntext3 = &quot;There are {item1} and {item2}.&quot;\r\nprint(text3.format(**{&quot;item1&quot;: item1, &quot;item2&quot;: item2}))\r\n\r\n# \u3044\u305a\u308c\u3082\u4ee5\u4e0b\u306e\u901a\u308a\u51fa\u529b\u3055\u308c\u308b\r\n# There are apple and banana.\r\n\r\n<\/pre>\n<h2>printf\u5f62\u5f0f<\/h2>\n<p>C\u8a00\u8a9e\u306eprintf\u5f62\u5f0f\u3068\u540c\u69d8\u306b%\u6f14\u7b97\u5b50\u306b\u5bfe\u3057\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u9806\u756a\u306b\u30bf\u30d7\u30eb\u3067\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u3068\u3001\u8f9e\u66f8\u3067\u30ad\u30fc\u3092\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u30572021\u5e74\u73fe\u5728\u3001\u516c\u5f0f\u3067\u3082\u300c\u3088\u304f\u3042\u308b\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u300d\u3068\u3044\u3063\u305f\u8a18\u8ff0\u304c\u3042\u308a\u3001\u4e0a\u3067\u7d39\u4ecb\u3057\u305f2\u3064\u306e\u65b9\u6cd5\u3068\u6bd4\u8f03\u3057\u3066\u5ec3\u308c\u3064\u3064\u3042\u308b\u5370\u8c61\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u53e4\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u306f\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u77e5\u8b58\u3068\u3057\u3066\u77e5\u3063\u3066\u304a\u304f\u3068\u30b3\u30fc\u30c9\u3092\u8aad\u307f\u89e3\u304f\u969b\u306b\u5f79\u306b\u7acb\u3064\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<blockquote><p>\n<a href=\"https:\/\/docs.python.org\/3.7\/library\/stdtypes.html#printf-style-string-formatting\" title=\"printf-style String Formatting\">printf-style String Formatting<\/a><br \/>\nNote 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.\n<\/p><\/blockquote>\n<h3>\u30bf\u30d7\u30eb<\/h3>\n<p>%s\u3092\u5217\u6319\u3057\u3001%\u6f14\u7b97\u5b50\u306e\u5f8c\u306b\u30bf\u30d7\u30eb\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ntext = &quot;There are %s, %s and %s.&quot;\r\nf_text = text % ('apple', 'bananas', 'oranges',)\r\nprint(f_text) # There are apple, bananas and oranges.\r\n<\/pre>\n<h3>\u8f9e\u66f8<\/h3>\n<p>%\u3068s\u306e\u9593\u306b\u4e38\u62ec\u5f27\u3067\u30ad\u30fc\u3092\u631f\u3080\u3068\u3001\u8f9e\u66f8\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u3001\"first\", \"second\", \"third\"\u304c\u305d\u308c\u305e\u308c\u30ad\u30fc\u306b\u306a\u308a\u307e\u3059\u3002\u8907\u96d1\u306a\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u306f\u3053\u3061\u3089\u3092\u4f7f\u7528\u3057\u305f\u307b\u3046\u304c\u3088\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ntext = &quot;There are %(first)s, %(second)s and %(third)s.&quot;\r\n\r\nf_text = text % {'first': 'apple', 'second': 'bananas', 'third': 'oranges'}\r\nprint(f_text) # There are apple, bananas and oranges.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u30da\u30fc\u30b8\u3067\u306fPython\u306e\u6587\u5b57\u5217\u3078\u306e\u5909\u6570\u57cb\u3081\u8fbc\u307f\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002 \u6587\u5b57\u5217\u3078\u306e\u5909\u6570\u57cb\u3081\u8fbc\u307f Python\u3067\u6587\u5b57\u5217\u306b\u5bfe\u3057\u3066\u5024\u3092\u57cb\u3081\u8fbc\u3080\u65b9\u6cd5\u306f\u3044\u304f\u3064\u304b\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u30da\u30fc\u30b8\u3067\u306f\u4ee5\u4e0b3\u3064\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002 ...<\/p>\n","protected":false},"author":1,"featured_media":3553,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[75],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/422"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=422"}],"version-history":[{"count":6,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/422\/revisions"}],"predecessor-version":[{"id":3554,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/422\/revisions\/3554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/3553"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}