Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

str

str is Python’s immutable Unicode text type and sequence of code points. Its constructor creates printable text from objects or decodes bytes when an encoding is supplied.

How it works

str is Python’s immutable text sequence type, storing Unicode code points. The constructor can either produce an object’s printable string form or decode bytes when an encoding is supplied; indexing returns strings of length one.

Syntax

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

Parameters

NameTypeDescription
objectobjectThe value to convert; omitted to produce an empty string.
encodingstrThe character encoding used when converting bytes to text.
errorsstrThe policy for handling encoding or decoding errors; defaults to strict.

Returns

A str representation of the object, or decoded text when encoding is supplied.

Key points

  • Use str for textual data in Python, including text received from files, network protocols, and APIs.
  • Single-, double-, and triple-quoted literals are available, allowing convenient representation of embedded quotes and multiline text.
  • Python has no character type: indexing a non-empty string yields another str containing one code point.
  • For repeated assembly, prefer join() or io.StringIO instead of repeatedly concatenating many fragments.
  • Calling str() with no argument creates an empty string.

Common mistakes

  • String contents cannot be changed in place; create a new string or use join() to combine fragments.
  • Without encoding or errors, str() uses the object’s informal string representation rather than decoding arbitrary bytes.
  • Custom objects without __str__() are represented through repr(), which may not be the intended user-facing format.

Example: Creating and using str

name = "Ada"
age = 36
text = f"{name} is {age}"

print(text[0])
print(str(age))
print(text == "Ada is 36")

Creates a Unicode string, indexes it to obtain a one-character string, and converts an integer to str.

When to use it

Store Unicode text

Representing names, labels, messages, and other textual application data.

Create printable representations

Converting numbers or custom values for display and logging.

Decode bytes

Turning encoded byte data into Python text at an I/O boundary.

Join fragments

Building output from many pieces efficiently.

Interview questions

QWhat is a str object in Python?

str represents textual data as an immutable sequence of Unicode code points. Because strings are sequences, they support indexing, slicing, and iteration; an indexed result is itself a string.

Follow-up: What does str(x) do when x is not already a string? It normally calls x.__str__(), falling back to repr(x) if no __str__() method exists.

QHow does the str constructor behave?

With only an object argument, str(object) returns its informal, printable representation. If no object is supplied, the result is the empty string.

Follow-up: How can bytes be converted to text? Supply an encoding, and optionally an error-handling policy, such as str(data, encoding="utf-8").

QHow do you decode bytes with str?

A str can be constructed from bytes by providing an encoding and optionally errors. The documented constructor defaults encoding to utf-8 and errors to strict in that form.

Follow-up: Why should decoding be explicit at boundaries? It makes the byte-to-text encoding choice visible and lets the caller select an appropriate error policy.

  • bytes
  • repr()
  • str.join()
  • io.StringIO
  • __str__()

Source: Built-in Types. Checked 2026-10-15.