I'm sure some of you started studying Python after learning Ruby. And while studying Python, I think you may be wondering, "How do you write that method in Ruby in Python?" This time, it is the to_s
version.
Ruby =>The value you want to convert to a string.to_s
Python => str(The value you want to convert to a string)
Example
#For Ruby
name = SampleUser
name.to_s
# "SampleUser"
#For Python
name = SampleUser
str(name)
# "SampleUser"
Quoted from @ shiracamus's comment (Thank you !!!)
Each object has a str and repr method. The str function (actually the str class) calls those methods appropriately, converts them into strings, and then converts them into character codes. Reference: https://docs.python.org/ja/3/library/stdtypes.html#str
>>> 123 .__str__()
'123'
>>> 3.14.__str__()
'3.14'
>>> None.__str__()
'None'
reference https://www.javadrive.jp/python/function/index2.html
Recommended Posts