Python: Convert to String
Convert value to string type
repr(x)
-
Convert to a string form that can be read back into Python or for
eval()
xx = [3, 4.3, (7, 8, "9")] print(repr(xx) == "[3, 4.3, (7, 8, '9')]")
str(x)
-
Convert to string in a human readable form.
Note: in practice,
str
andrepr
returns the same thing.xx = [3, 4.3, (7, 8, "9")] print(str(xx) == "[3, 4.3, (7, 8, '9')]")
for more flexible way, use the format
method.
[see Python: Format String]