Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between str () and repr () in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report