Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Bool (True/False) to a String in Python?

#1
How to Convert Bool (True/False) to a String in Python?

<div>
<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;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" 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>
<p>Note that this tutorial doesn’t concern <em>“concatenating a Boolean to a string”</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>
<h2>Simple Bool to String Conversion</h2>
<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>
<p>Here’s a minimal example:</p>
<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'
>>> str(False) 'False'</pre>
<h2>Python Boolean Type is Integer</h2>
<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>
<p>Here’s a minimal example:</p>
<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
True
>>> False == 0
True</pre>
<h2>Convert True to ‘1’ and False to ‘0’</h2>
<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’s use of integers to represent Boolean values. </p>
<p>Here’s a minimal example:</p>
<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'
>>> str(int(False)) '0'</pre>
<h2>Convert List of Boolean to List of Strings</h2>
<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>
<p>Here’s a simple example:</p>
<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]
my_strings = [str(x) for x in my_bools]
print(my_strings)
# ['True', 'True', 'False', 'False', 'True']
</pre>
<h2>Convert String Back to Boolean</h2>
<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>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" 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>
<p>Here’s the short summary:</p>
<p>You can convert a string value <code>s</code> to a Boolean value using the Python function <code>bool(s)</code>. </p>
<p>For example, <code>bool('True')</code> and <code>bool('1')</code> return <code>True</code>. </p>
<p>However, <code>bool('False')</code> and <code>bool('0')</code> return <code>False</code> as well which may come unexpected to you. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> This is because all Python objects are “truthy”, 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>
<p>You can see this in the following example:</p>
<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')
True
>>> bool('1')
True
>>> bool('2')
True
>>> bool('False')
True
>>> bool('0')
True
>>> bool('')
False</pre>
<p>Okay, what to do about it? </p>
<p class="has-global-color-8-background-color has-background">Easy – 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>
<p>Finally – this behavior is as expected by many coders just starting out.</p>
<p>Here’s an example:</p>
<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'))
False
>>> bool(eval('0'))
False
>>> bool(eval('True'))
True
>>> bool(eval('1'))
True</pre>
<p>Feel free to go over our detailed guide on the function:</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" 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>
<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 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>
</div>


https://www.sickgaming.net/blog/2022/10/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016