Published Date : 2020年4月6日13:41

Python : ブーリアン型変数を文字列型変数にキャストする
Python : Cast a Boolean variable to type String

This blog has an English translation

YouTubeにアップした動画、「【Python】Part 2 - 一分間の3Dアニメーションで理解するPythonの基礎」の補足説明の記事です。

Here's a little more about the "【Python】Part 2 - Understand the basics of Python with a one-minute 3D animation" video I uploaded to YouTube.


Suppose you want to display Boolean (Contains True or False) variables as characters.

>>> bool_value = True
>>> print(bool_value)


It is displayed as "print(Boolean variable)" using the print() function as usual, but what if you want it to be concatenated with a string object?

>>> bool_value = True
>>> print("I'm a Python beginner = " + bool_value)
TypeError: can only concatenate str (not "bool") to str
【Python】Part 2 - Understand the basics of Python with a one-minute 3D animation


I'll come to the conclusion. All you have to do is use the str() method on a Boolean variable.

>>> 日本語 = True

>>> print("'日本語'という変数には" + 日本語 + "が入っています。")
TypeError: can only concatenate str (not "bool") to str

>>> print("'日本語'という変数には" + str(日本語) + "が入っています。")
>>> japanese = True

>>> print("The value stored in variable 'japanese' is "+ japanese + " .")
TypeError: can only concatenate str (not "bool") to str

>>> print("The value stored in variable 'japanese' is "+ str(japanese) + " .")
The value stored in variable 'japanese' is True

Why did TypeError occur?


In the video, I used the difference in shape and size of the box containing the variable to explain why this TypeError occured.


In fact, the first thing to explain is that all Python variables are objects.


And objects are created from classes.


Furthermore, objects created from classes have methods and attribute values (Properties).


Special methods, such as __str__, must be implemented to display the object as "String".


And if this __str__() special method is implemented, the print() and str() functions will call __str__() automatically.


Try it out by writing a simple script.

>>> test = True
>>> print(test)

>>> class Test1:
...    def __init__(self, bool_value):
...        self.value = bool_value
...    def __str__(self):
...        return f"{self.value}"

>>> test1 = Test1(True)
>>> print(test1)

>>> class Test2:
...    def __init__(self, bool_value):
...        self.value = bool_value

>>> test2 = Test2(True)
>>> print(test2)
<__main__.Test2 object at 0x01B030C0>


So what happens when you try to add strings?

print("This object (" + test1 + ") has an __str__ method")
TypeError: can only concatenate str (not "Test1") to str


The reason for the error, despite using the print() function, is that the contents of the print() function had a type error before it worked.


Now call the __str__ method and try again.

print("This object (" + test1.__str__() + ") has an __str__ method")
This object (True) has an __str__ method


The str() function does the same thing.

print("This object (" + str(test1) + ") has an __str__ method")
This object (True) has an __str__ method


If the object does not implement the __str__ method, it behaves as follows.


'<__main__.Test2 object at 0x01B030C0>'

print("This object (" + str(test2) + ") has not an __str__ method")
This object (<__main__.Test2 object at 0x01B030C0>) has not an __str__ method

See You Next Page!