{"id":964,"date":"2020-09-03T10:05:09","date_gmt":"2020-09-03T10:05:09","guid":{"rendered":"https:\/\/www.webhozz.com\/code\/?p=964"},"modified":"2021-04-01T12:43:22","modified_gmt":"2021-04-01T12:43:22","slug":"format-string-di-python","status":"publish","type":"post","link":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/","title":{"rendered":"Format String Pada Python"},"content":{"rendered":"\n<p>Untuk membuktikan\napakah string yang akan ditampilkan seperti yang diharapkan, kita dapat\nmemformat hasilnya dengan&nbsp;metode <code>format() <\/code>ini.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Format string ()<\/strong><\/h4>\n\n\n\n<p>Metode&nbsp;<code>format() <\/code>memungkinkan\nkamu untuk format yang dipilih dari bagian string.<\/p>\n\n\n\n<p>Terkadang ada bagian teks yang\ntidak kamu control, mungkin berasal dari basis data, atau input pengguna?<\/p>\n\n\n\n<p>Untuk mengontrol nilai-nilai\ntersebut, tambahkan placeholder (kurung keriting&nbsp;<code>{}<\/code>) dalam teks, dan\njalankan nilai melalui&nbsp;metode<code> format()<\/code>:<\/p>\n\n\n\n<p><strong>Contoh<\/strong><\/p>\n\n\n\n<p>Tambahkan placeholder di tempat yang kamu ingin menampilkan harga:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprice = 49\ntxt = &quot;The price is {} dollars&quot;\nprint(txt.format(price))\n<\/pre><\/div>\n\n\n<p>Kamu dapat menambahkan parameter\ndi dalam kurung keriting untuk menentukan cara mengkonversi nilai:<\/p>\n\n\n\n<p><strong>Contoh<\/strong><\/p>\n\n\n\n<p>Format harga yang akan ditampilkan sebagai angka dengan dua desimal:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntxt = &quot;The price is {:.2f} dollars&quot;\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>Kelipatan Nilai<\/strong><\/h4>\n\n\n\n<p>Jika kamu ingin menggunakan lebih banyak nilai, tambahkan saja nilai lebih ke metode format ():<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(txt.format(price, itemno, count))\n<\/pre><\/div>\n\n\n<p>Dan tambahkan lebih banyak placeholders:<\/p>\n\n\n\n<p><strong>Contoh<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nquantity = 3\nitemno = 567\nprice = 49\nmyorder = &quot;I want {} pieces of item number {} for {:.2f} dollars.&quot;\nprint(myorder.format(quantity, itemno, price))\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>Angka Indeks<\/strong><\/h4>\n\n\n\n<p>Kamu dapat menggunakan nomor\nindeks (angka di dalam kurung keriting&nbsp;<code>{0}<\/code>) untuk memastikan nilai\nditempatkan di placeholder yang benar:<\/p>\n\n\n\n<p><strong>Contoh<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nquantity = 3\nitemno = 567\nprice = 49\nmyorder = &quot;I want {0} pieces of item number {1} for {2:.2f} dollars.&quot;\nprint(myorder.format(quantity, itemno, price))\n<\/pre><\/div>\n\n\n<p>Juga, jika kmau ingin merujuk ke\nnilai yang sama lebih dari sekali, gunakan nomor indeks:<\/p>\n\n\n\n<p><strong>Contoh<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nage = 36\nname = &quot;John&quot;\ntxt = &quot;His name is {1}. {1} is {0} years old.&quot;\nprint(txt.format(age, name))\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>Nama Indeks<\/strong><\/h4>\n\n\n\n<p>Kamu juga dapat menggunakan nama\nindex dengan memasukkan nama di dalam kurung keriting&nbsp;<code>{carname}<\/code>,\ntetapi kemudian kmau harus menggunakan nama saat kamu melewati nilai\nparameter&nbsp;<code>txt.format(carname\n= \"Ford\")<\/code>:<\/p>\n\n\n\n<p><strong>Contoh<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmyorder = &quot;I have a {carname}, it is a {model}.&quot;\nprint(myorder.format(carname = &quot;Ford&quot;, model = &quot;Mustang&quot;))\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Untuk membuktikan apakah string yang akan ditampilkan seperti yang diharapkan, kita dapat memformat hasilnya dengan&nbsp;metode format() ini. Format string () Metode&nbsp;format() memungkinkan kamu untuk format<\/p>\n","protected":false},"author":1,"featured_media":806,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Belajar Format String Pada Python - WebHozz Code<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webhozz.com\/code\/format-string-di-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Belajar Format String Pada Python - WebHozz Code\" \/>\n<meta property=\"og:description\" content=\"Untuk membuktikan apakah string yang akan ditampilkan seperti yang diharapkan, kita dapat memformat hasilnya dengan&nbsp;metode format() ini. Format string () Metode&nbsp;format() memungkinkan kamu untuk format\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webhozz.com\/code\/format-string-di-python\/\" \/>\n<meta property=\"og:site_name\" content=\"WebHozz Code\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-03T10:05:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-01T12:43:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2020\/01\/Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/04c781607b26b4f4f052684571acc0c6\"},\"headline\":\"Format String Pada Python\",\"datePublished\":\"2020-09-03T10:05:09+00:00\",\"dateModified\":\"2021-04-01T12:43:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/\"},\"wordCount\":191,\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Python.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/\",\"name\":\"Belajar Format String Pada Python - WebHozz Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Python.jpg\",\"datePublished\":\"2020-09-03T10:05:09+00:00\",\"dateModified\":\"2021-04-01T12:43:22+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/04c781607b26b4f4f052684571acc0c6\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Python.jpg\",\"contentUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Python.jpg\",\"width\":750,\"height\":400},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/format-string-di-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Format String Pada Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\",\"name\":\"WebHozz Code\",\"description\":\"Tutorial Web &amp; Pemrograman Indonesia\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/04c781607b26b4f4f052684571acc0c6\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Belajar Format String Pada Python - WebHozz Code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/","og_locale":"en_US","og_type":"article","og_title":"Belajar Format String Pada Python - WebHozz Code","og_description":"Untuk membuktikan apakah string yang akan ditampilkan seperti yang diharapkan, kita dapat memformat hasilnya dengan&nbsp;metode format() ini. Format string () Metode&nbsp;format() memungkinkan kamu untuk format","og_url":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/","og_site_name":"WebHozz Code","article_published_time":"2020-09-03T10:05:09+00:00","article_modified_time":"2021-04-01T12:43:22+00:00","og_image":[{"width":750,"height":400,"url":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2020\/01\/Python.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/#article","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/"},"author":{"name":"admin","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/04c781607b26b4f4f052684571acc0c6"},"headline":"Format String Pada Python","datePublished":"2020-09-03T10:05:09+00:00","dateModified":"2021-04-01T12:43:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/"},"wordCount":191,"image":{"@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2020\/01\/Python.jpg","articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/","url":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/","name":"Belajar Format String Pada Python - WebHozz Code","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/#primaryimage"},"image":{"@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2020\/01\/Python.jpg","datePublished":"2020-09-03T10:05:09+00:00","dateModified":"2021-04-01T12:43:22+00:00","author":{"@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/04c781607b26b4f4f052684571acc0c6"},"breadcrumb":{"@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webhozz.com\/code\/format-string-di-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/#primaryimage","url":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2020\/01\/Python.jpg","contentUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2020\/01\/Python.jpg","width":750,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/www.webhozz.com\/code\/format-string-di-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webhozz.com\/code\/"},{"@type":"ListItem","position":2,"name":"Format String Pada Python"}]},{"@type":"WebSite","@id":"https:\/\/www.webhozz.com\/code\/#website","url":"https:\/\/www.webhozz.com\/code\/","name":"WebHozz Code","description":"Tutorial Web &amp; Pemrograman Indonesia","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webhozz.com\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/04c781607b26b4f4f052684571acc0c6","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.webhozz.com\/code\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/comments?post=964"}],"version-history":[{"count":3,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/964\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/964\/revisions\/1128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/media\/806"}],"wp:attachment":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/media?parent=964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/categories?post=964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/tags?post=964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}