Home/Learn/Python A–Z/Numbers, Strings & f-strings

Numbers, Strings & f-strings

Beginner
Fundamentals

Python ints have unlimited precision, floats are IEEE-754 doubles, and strings are immutable sequences with a rich method set — formatted beautifully with f-strings.

Overview

Python's int grows as big as memory allows — no overflow, no long/BigInteger distinction like Java (2**1000 just works). Floats are standard 64-bit doubles with the usual 0.1 + 0.2 != 0.3 caveat (use decimal for money). Strings are immutable: every "modification" creates a new string. The modern way to build strings is the f-string — expression interpolation with formatting controls that you will use in every program you ever write.

Numbers Without Overflow

Integer division (//), true division (/), modulo (%), and power (**) are the operators interviews test. Note that / ALWAYS returns float — a common bug when porting Java DSA solutions to Python.

Division traps — // floors, / gives float
print(7 / 2)      # 3.5   — true division, always float
print(7 // 2)     # 3     — floor division
print(-7 // 2)    # -4    — floors toward negative infinity! (Java: -3)
print(7 % 2)      # 1
print(2 ** 100)   # 1267650600228229401496703205376 — no overflow

# Float precision — never compare floats with ==
print(0.1 + 0.2 == 0.3)          # False!
import math
print(math.isclose(0.1 + 0.2, 0.3))  # True — the right way

# Money? Use Decimal
from decimal import Decimal
print(Decimal("0.1") + Decimal("0.2"))  # 0.3 exactly

Strings & f-strings

Strings support indexing, slicing, and dozens of methods. f-strings (f"...") embed any expression in {} with optional format specs — width, decimals, padding. Since strings are immutable, use "".join(parts) to build large strings in loops, not repeated +.

f-strings + slicing — used in every interview answer
name, score = "Priya", 93.4567

# f-strings — the only formatting you need
print(f"{name} scored {score:.2f}%")        # Priya scored 93.46%
print(f"{name:>10}|")                        # '     Priya|' right-align
print(f"{1234567:,}")                        # 1,234,567
print(f"{name=}")                            # name='Priya' (debugging!)

s = "AiCanCode"
print(s[0], s[-1])       # A e     — negative index from the end
print(s[2:5])            # Can     — slice [start:stop)
print(s[::-1])           # edoCnaCiA — reversed via slicing
print(s.lower(), s.upper(), s.startswith("Ai"))

# Building strings efficiently
words = ["campus", "to", "corporate"]
print(" ".join(words))   # campus to corporate  — O(n), not O(n²)

Key Points to Remember

  • 1int is arbitrary precision — 2**1000 works; no integer overflow in Python
  • 2/ always returns float; // floors (careful: -7 // 2 == -4, unlike Java)
  • 3Strings are immutable — s[0] = "x" is a TypeError; use join() to build strings in loops
  • 4f-strings: f"{value:.2f}", f"{x=}" for debug, f"{n:,}" for thousands separators

Interview Questions

Sign in to ask Aria
1

Why does 0.1 + 0.2 == 0.3 return False? How do you compare floats correctly?

EasyPaytm
2

Strings are immutable in Python — what does that mean for building a string in a loop, and what is the efficient pattern?

MediumAmazon
3

What does -7 // 2 return in Python and why? How does it differ from Java?

MediumMicrosoft

Ask Aria about Numbers, Strings & f-strings

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…