How to write Ruby to_s in Python

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.

Conclusion

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"

Postscript

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

How to write Ruby to_s in Python
Difference in how to write if statement between ruby ​​and python
How to write the correct shebang in Perl, Python and Ruby scripts
20th Offline Real-time How to Write Problems in Python
How to write string concatenation in multiple lines in Python
How to handle JSON in Ruby, Python, JavaScript, PHP
How to write a Python class
[Python] How to do PCA in Python
How to write soberly in pandas
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
[Python] How to write an if statement in one sentence.
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to write Python document comments (Docstrings)
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to write this process in Perl?
How to use is and == in Python
Offline real-time how to write E11 ruby and python implementation example
The 15th offline real-time how to write reference problem in Python
How to write offline real time Solve E04 problems in Python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
How to install Python
Write Python in MySQL
How to use the C library in Python
How to receive command line arguments in Python
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
Summary of how to use MNIST in Python
How to write regular expression patterns in Linux
How to specify TLS version in python requests
The trick to write flatten concisely in python
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
To write to Error Repoting in Python on GAE