Basic Python

str VS repr

Naranjito 2023. 11. 16. 10:01
  • repr

It returns the string wrapped with (').

>>> a = "Life is too short"
>>> str(a)
'Life is too short'

>>> repr(a)
"'Life is too short'"

 

>>> import datetime
>>> a = datetime.datetime(2017, 9, 27)
>>> str(a)
'2017-09-27 00:00:00'

>>> repr(a)
'datetime.datetime(2017, 9, 27, 0, 0)'
str repr
-human-readable
-informal
-called by the built-in print(), str()
-intended for users
-more information-rich
-official
-called by the built-in repr()
- intended for developers

 

https://www.digitalocean.com/community/tutorials/python-str-repr-functions

'Basic Python' 카테고리의 다른 글

argparse  (0) 2023.12.13
Logger, Handler, Filter, Formatter  (0) 2023.12.08
getattr  (0) 2023.11.15
sort VS sorted  (0) 2023.11.10
abspath, chdir, basename, os.path.join  (0) 2023.11.07