str() String constructor

Last updated: 2026-09-22

str() String constructor

str() converts an object to str; with no args returns ''.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

str(object='')
str(object=b'', encoding='utf-8', errors='strict')

⚙️ Parameters

object OptionalThe object to convert; omitted returns ''.(Default:'')
encoding OptionalEncoding used when decoding bytes.(Default:'utf-8')
errors OptionalError handling for decoding (strict/ignore/replace, etc.).(Default:'strict')

Returns:The string form of the object; bytes are decoded using the given encoding.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

42
[1, 2]
中
More case studies coming soon — check back later.

❓ FAQ

QWhat do str(42) and str([1, 2]) return?
AThey return '42' and '[1, 2]' — str() converts any object into a readable string.
QWhat does str() return with no arguments?
AIt returns an empty string ''.
QWhat does str(b'\xe4\xb8\xad', encoding='utf-8') return?
AIt returns '中'. A bytes object must be given an encoding to decode into a string; without encoding you only get a literal form like "b'\\xe4\\xb8\\xad'".
QWhat is the difference between str() and repr()?
Astr() is for humans (used by print by default), while repr() is for developers and aims to be reconstructible; e.g. str('a') is a while repr('a') is "'a'"
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏