str()

Python · Variables & Data Types

Theory

str() returns a text version of any value. Python asks the object for its readable string form (its __str__ method), so numbers, booleans, None, lists and dicts all work: str(25) is '25', str(None) is 'None' and str([1, 2]) is '[1, 2]'. The original value is untouched; you get a brand-new string object.

The most common reason you need it is that + between a string and a number is a TypeError: 'Age: ' + 25 fails because Python refuses to guess whether you meant math or text. 'Age: ' + str(25) works. A related function, repr(), returns the developer-facing form that includes quotes for strings, which is what you see when a list is printed. In modern code, f-strings like f'Age: {age}' call str() for you and are usually cleaner than manual conversion.

Why we use it

Programs constantly turn values into text for messages, file names, log lines and reports. str() is the explicit, safe way to make that conversion and fix the classic 'can only concatenate str (not "int") to str' error.

When to use it

Use str() when joining a number into a string with + or when a function needs text, such as writing to a file. When building whole sentences, an f-string is usually more readable. Use repr() when debugging, to see quotes and hidden characters.

How it works

  1. str(value) is called
  2. Python calls the object's __str__ method
  3. Does the type define a custom __str__? Yes: Use that readable text. No: Fall back to repr(), the developer form.
  4. Create a new str object; the original value is unchanged
  5. e.g. 'Age: ' + str(25) gives 'Age: 25'

Syntax

str(value)

Example

age = 25
message = "Age: " + str(age)
print(message)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic