What is the difference between str () and repr () in python
What this article shares to you is about what is the difference between str () and repr () in python. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Create a new python file named py3_str_vs_repr.py, and write the operation code in this file:
A = [1BI 2] 3 str 4] b = 'Hello Moto'print (str (a)) print (repr (a)) # list: it is found that the results printed with str (b) and repr () # are the same as those printed with print (str (b)) print (repr (b)) # String:str () with single quotes # repr () printed results without double quotes # the code above does not seem to show the difference between # str () and repr () # root According to the official explanation # str () goal is readable printing # repr () goal is to print clear information # for example: import datetime
Times = datetime.datetime (print (str (times)) # 2019-05-29 21:34:00.010000print (repr (times)) # datetime.datetime # so repr () is more aimed at developers to debug programs and use # str () with end users or non-developers.
Running effect:
[1, 2, 3, 4] [1, 2, 3, 4] Hello Moto'Hello Moto'2019-05-29 21:34:00.010000datetime.datetime (2019, 5, 29, 21, 34, 0, 10000) is the difference between str () and repr () in python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.