{"id":128577,"date":"2022-10-03T08:50:15","date_gmt":"2022-10-03T08:50:15","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=744809"},"modified":"2022-10-03T08:50:15","modified_gmt":"2022-10-03T08:50:15","slug":"how-to-convert-bool-true-false-to-a-string-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/03\/how-to-convert-bool-true-false-to-a-string-in-python\/","title":{"rendered":"How to Convert Bool (True\/False) to a String in Python?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;744809&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: Given a Boolean value <code>True<\/code> or <code>False<\/code>. How to convert it to a string <code>\"True\"<\/code> or <code>\"False\"<\/code> in Python?<\/p>\n<p>Note that this tutorial doesn&#8217;t concern <em>&#8220;concatenating a Boolean to a string&#8221;<\/em>. If you want to do this, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-concatenate-a-boolean-to-a-string-in-python\/\" data-type=\"post\" data-id=\"744822\" target=\"_blank\">check out our in-depth article on the Finxter blog<\/a>.<\/p>\n<h2>Simple Bool to String Conversion<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">To convert a given Boolean value to a string in Python, use the <code><a href=\"https:\/\/blog.finxter.com\/python-str-function\/\" data-type=\"post\" data-id=\"23735\" target=\"_blank\" rel=\"noreferrer noopener\">str(boolean)<\/a><\/code> function and pass the Boolean value into it. This converts Boolean <code>True<\/code> to string <code>\"True\"<\/code> and Boolean <code>False<\/code> to string <code>\"False\"<\/code>. <\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> str(True) 'True'\n>>> str(False) 'False'<\/pre>\n<h2>Python Boolean Type is Integer<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">Booleans are represented by integers in Python, i.e., <code>bool<\/code> is a subclass of <code>int<\/code>. Boolean value <code>True<\/code> is represented with integer <code>1<\/code>. And Boolean value <code>False<\/code> is represented with integer <code>0<\/code>. <\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> True == 1\nTrue\n>>> False == 0\nTrue<\/pre>\n<h2>Convert True to &#8216;1&#8217; and False to &#8216;0&#8217;<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">To convert a Boolean value to a string <code>'1'<\/code> or <code>'0'<\/code>, use the expression <code>str(int(boolean))<\/code>. For instance, <code>str(int(True))<\/code> returns <code>'1'<\/code> and <code>str(int(False))<\/code> returns <code>'0'<\/code>. This is because of Python&#8217;s use of integers to represent Boolean values. <\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> str(int(True)) '1'\n>>> str(int(False)) '0'<\/pre>\n<h2>Convert List of Boolean to List of Strings<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">To convert a Boolean to a string list, use the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"post\" data-id=\"1171\" target=\"_blank\">list comprehension<\/a> expression <code>[str(x) for x in my_bools]<\/code> assuming the Boolean list is stored in variable <code>my_bools<\/code>. This converts each Boolean <code>x<\/code> to a string using the built-in <code>str()<\/code> function and repeats it for all <code>x<\/code> in the Boolean <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\" target=\"_blank\" rel=\"noreferrer noopener\">list<\/a>.<\/p>\n<p>Here&#8217;s a simple example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_bools = [True, True, False, False, True]\nmy_strings = [str(x) for x in my_bools]\nprint(my_strings)\n# ['True', 'True', 'False', 'False', 'True']\n<\/pre>\n<h2>Convert String Back to Boolean<\/h2>\n<p>What if you want to convert the string representation <code>'True'<\/code> and <code>'False'<\/code> (or: <code>'1'<\/code> and <code>'0'<\/code>) back to the Boolean representation <code>True<\/code> and <code>False<\/code>?<\/p>\n<p class=\"has-base-2-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/how-to-convert-a-string-to-a-boolean-in-python\/\" data-type=\"post\" data-id=\"4203\" target=\"_blank\" rel=\"noreferrer noopener\">String to Boolean Conversion<\/a><\/p>\n<p>Here&#8217;s the short summary:<\/p>\n<p>You can convert a string value <code>s<\/code> to a Boolean value using the Python function <code>bool(s)<\/code>. <\/p>\n<p>For example, <code>bool('True')<\/code> and <code>bool('1')<\/code> return <code>True<\/code>. <\/p>\n<p>However, <code>bool('False')<\/code> and <code>bool('0')<\/code> return <code>False<\/code> as well which may come unexpected to you. <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> This is because all Python objects are &#8220;truthy&#8221;, i.e., they have an associated Boolean value. As a rule of thumb: empty values return Boolean <code>True<\/code> and non-empty values return Boolean <code>False<\/code>. So, only <code>bool('')<\/code> on the empty string <code>''<\/code> returns <code>False<\/code>. All other strings return <code>True<\/code>!<\/p>\n<p>You can see this in the following example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> bool('True')\nTrue\n>>> bool('1')\nTrue\n>>> bool('2')\nTrue\n>>> bool('False')\nTrue\n>>> bool('0')\nTrue\n>>> bool('')\nFalse<\/pre>\n<p>Okay, what to do about it? <\/p>\n<p class=\"has-global-color-8-background-color has-background\">Easy &#8211; first pass the string into the <code><a href=\"https:\/\/blog.finxter.com\/python-eval\/\" data-type=\"post\" data-id=\"19204\" target=\"_blank\" rel=\"noreferrer noopener\">eval()<\/a><\/code> function and then pass the result into the <code>bool()<\/code> function. In other words, the expression <code>bool(eval(my_string))<\/code> converts a string to a Boolean mapping <code>'True'<\/code> and <code>'1'<\/code> to Boolean <code>True<\/code> and <code>'False'<\/code> and <code>'0'<\/code> to Boolean <code>False<\/code>. <\/p>\n<p>Finally &#8211; this behavior is as expected by many coders just starting out.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> bool(eval('False'))\nFalse\n>>> bool(eval('0'))\nFalse\n>>> bool(eval('True'))\nTrue\n>>> bool(eval('1'))\nTrue<\/pre>\n<p>Feel free to go over our detailed guide on the function:<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/python-eval\/\" data-type=\"post\" data-id=\"19204\" target=\"_blank\" rel=\"noreferrer noopener\">Python <code>eval()<\/code> deep dive<\/a><\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-convert-bool-true-false-to-a-string-in-python\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F2SV60ENwXVw%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Question: Given a Boolean value True or False. How to convert it to a string &#8220;True&#8221; or &#8220;False&#8221; in Python? Note that this tutorial doesn&#8217;t concern &#8220;concatenating a Boolean to a string&#8221;. If you want to do this, check out our in-depth article on the Finxter blog. Simple Bool to String [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128577","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128577"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128577\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}