DevHireLab
Tutorials
BootcampProblemsCode SimulatorAI InterviewSoonContact
DevHireLab
Tutorials
BootcampProblemsCode SimulatorAI InterviewSoonContact
Getting Started
01
What is Python and Why Learn It in 2026
02
Installing Python on Windows, Mac, and Linux
03
Setting up VS Code / PyCharm for Python
04
Understanding the Python Interpreter and REPL
05
Writing and Running Your First Python Program
06
Python Syntax and Indentation Rules
Variables and Data Types
01
Variables and Naming Conventions in Python
02
Numbers in Python (int, float, complex)
03
Strings and String Methods in Python
04
Booleans in Python
05
Type Conversion / Casting in Python
06
Understanding the None Type in Python
Operators
01
Arithmetic Operators in Python
02
Comparison Operators in Python
03
Logical Operators in Python
04
Assignment Operators in Python
05
Bitwise Operators in Python
06
Identity (is) and Membership (in) Operators
07
Walrus Operator (:=) in Python
Input, Output & Strings
01
input() and print() Functions in Python
02
f-strings and String Formatting in Python
03
String Slicing and Indexing in Python
04
Common String Methods (split, join, strip, replace)
05
Escape Characters in Python Strings
Control Flow
01
if, elif, else Statements in Python
02
Nested Conditionals in Python
03
for Loops in Python
04
while Loops in Python
05
break, continue, and pass Statements
06
Loop with else Clause in Python
07
match-case (Structural Pattern Matching)
Data Structures
01
Python Lists: Creation, Indexing, Slicing, Methods
02
Python Tuples and Immutability
03
Python Sets and Set Operations
04
Python Dictionaries: Keys, Values, and Methods
05
List Comprehensions in Python
06
Dictionary & Set Comprehensions in Python
07
Working with Nested Lists and Dictionaries
Functions
01
Defining and Calling Functions in Python
02
Function Arguments: Positional, Keyword, Default
03
*args and **kwargs in Python
04
Return Statements in Python Functions
05
Lambda Functions in Python
06
Recursion in Python
07
Variable Scope: Local, Global, Nonlocal
08
Docstrings and Function Annotations
Functional Programming Basics
01
map(), filter(), and reduce() in Python
02
Closures in Python Explained
03
Python Decorators: Basic to Advanced
04
Generators and yield in Python
Object-Oriented Programming (OOP)
01
Classes and Objects in Python
02
The init Constructor in Python
03
Instance vs Class Variables in Python
04
Inheritance in Python (Single, Multiple, Multilevel)
05
Polymorphism in Python
06
Encapsulation in Python
07
Abstraction in Python with the abc Module
08
Magic/Dunder Methods in Python
09
Static and Class Methods in Python
10
The @property Decorator in Python
11
Python Dataclasses Explained
Error Handling
01
try, except, finally in Python
02
Handling Multiple Exceptions in Python
03
Raising Custom Exceptions in Python
04
Understanding Python's Exception Hierarchy
05
Assertions in Python
File Handling
01
Reading and Writing Text Files in Python
02
Working with the with Statement in Python
03
Working with CSV Files in Python
04
Working with JSON Files in Python
05
Working with Directories (os, pathlib) in Python
Modules and Packages
01
Importing Built-in Modules in Python
02
Creating Your Own Modules in Python
03
Understanding Packages and init.py
04
Python Standard Library Overview
05
Installing Packages with pip
Advanced Core Concepts
01
Iterators and Iterables in Python
02
Context Managers in Python (with, contextlib)
03
Multithreading Basics in Python
04
Multiprocessing Basics in Python
05
Asyncio Basics: async/await in Python
06
Memory Management and Garbage Collection in Python
07
Type Hints in Python (typing module)

Strings and String Methods in Python

Text is everywhere in programming — names, messages, file paths, user input. Python strings are how the language handles all of it, and they come with a large, genuinely useful set of built-in tools. This article covers how strings work, how to slice and search them, the essential python string methods, and modern formatting with f-strings.

Introduction: What is a String in Python?

A string is simply a sequence of characters enclosed in quotes:

name = "Alex"
message = 'Hello there'
Single, double, and triple quotes

Python accepts single quotes and double quotes interchangeably — pick one and stay consistent, or use whichever avoids escaping a quote inside the string itself:

quote = "She said 'hello' to me"    # double quotes let the single quotes pass through untouched

Triple quotes ("""...""" or '''...''') serve a different purpose: multi-line strings.

paragraph = """This spans
multiple lines
without needing any special escape characters."""

Triple-quoted strings are also what Python uses for docstrings, as covered in the earlier article on syntax basics.

No separate character type

Unlike some languages, Python has no dedicated "char" type. A single character is just a string of length one:

letter = "A"
print(type(letter))   # <class 'str'> — same type as a full sentence

Accessing Strings: Indexing and Slicing

Strings behave like sequences, meaning you can access individual characters or ranges of characters by position.

Positive and negative indexing
word = "Python"

print(word[0])    # P — first character
print(word[-1])   # n — last character

Positive indices count from the start (starting at 0), while negative indices count backward from the end, which is a handy shortcut for grabbing the last item without knowing the string's length.

Python string slicing

Slicing lets you pull out a range of characters using the syntax s[start:end:step]:

word = "Python"

print(word[0:3])    # Pyt — characters from index 0 up to (not including) 3
print(word[2:])     # thon — from index 2 to the end
print(word[:4])     # Pyth — from the start up to index 4
print(word[::2])    # Pto — every second character
print(word[::-1])   # nohtyP — the whole string, reversed

That last example — s[::-1] — is a common idiom for reversing a string: no start or end specified (so it covers the whole string), and a step of -1 to walk backward.

Strings are immutable

This is one of the most important things to internalize early: strings cannot be changed in place. Every string method that appears to "modify" a string actually returns a brand-new string, leaving the original untouched.

word = "python"
word[0] = "P"   # TypeError — strings don't support item assignment

The correct approach is to create a new string, either by slicing and concatenating or by using a method that returns the modified version:

word = "python"
word = "P" + word[1:]   # build a new string
print(word)   # Python

Essential String Methods

Python strings come with dozens of built-in methods. Here are the ones you'll reach for constantly.

Case conversion
text = "Hello World"

print(text.upper())        # HELLO WORLD
print(text.lower())        # hello world
print(text.capitalize())   # Hello world — only the first character capitalized
print(text.title())        # Hello World — first letter of every word capitalized
Whitespace handling
messy = "   hello   "

print(messy.strip())    # "hello" — removes whitespace from both ends
print(messy.lstrip())   # "hello   " — removes only from the left
print(messy.rstrip())   # "   hello" — removes only from the right

strip() is especially useful when cleaning up user input, which often carries accidental leading or trailing spaces.

Searching and counting
sentence = "the quick brown fox"

print(sentence.find("quick"))    # 4 — starting index of the match
print(sentence.index("quick"))   # 4 — same result, but raises an error if not found
print(sentence.count("o"))       # 2 — number of occurrences

print("fox" in sentence)         # True — membership check with the `in` keyword

find() and index() do almost the same thing, with one key difference: find() returns -1 if the substring isn't found, while index() raises a ValueError. Use in first if you just need a yes/no answer.

Splitting and joining
csv_line = "apple,banana,cherry"

fruits = csv_line.split(",")
print(fruits)   # ['apple', 'banana', 'cherry']

joined = "-".join(fruits)
print(joined)   # apple-banana-cherry

split() breaks a string into a list based on a separator (defaulting to whitespace if none is given). join() does the reverse — it stitches a list of strings back together, with whatever separator you call it on.

Replacing
text = "I like cats"
print(text.replace("cats", "dogs"))   # I like dogs

Like every other string method, replace() returns a new string rather than modifying the original.

String Formatting: f-strings and Beyond

f-strings as the modern standard

f-strings are the current standard for python string formatting — they let you embed variables and expressions directly inside a string, prefixed with f:

name = "Alex"
age = 30
print(f"{name} is {age} years old")
# Alex is 30 years old

You're not limited to simple variables — any valid expression can go inside the curly braces:

price = 49.5
print(f"Total with tax: {price * 1.08}")
Formatting numbers inside f-strings

You can also control number formatting directly within the braces using a format specifier after a colon:

price = 49.5
print(f"")   # $49.50 — rounds to 2 decimal places

This is one of the most common patterns you'll write — formatting a float to a fixed number of decimal places for display.

Older formatting methods, for context

Before f-strings arrived, Python code relied on two other approaches, and you'll still encounter both in existing codebases:

name = "Alex"

# .format() method
print("{} is here".format(name))

# % operator (the oldest style)
print("%s is here" % name)

Both still work, but f-strings are preferred now — they're more concise, easier to read (the variable sits right where it's used, instead of separated at the end), and generally faster at runtime.

Common String Operations & Best Practices

Concatenation: + vs. join()

For combining a small, fixed number of strings, + is perfectly fine:

greeting = "Hello, " + name + "!"

But for combining many strings — especially inside a loop — join() is significantly more efficient. Because strings are immutable, each + operation creates an entirely new string in memory; repeating that in a loop gets expensive fast. join() avoids that overhead:

words = ["This", "is", "much", "more", "efficient"]
sentence = " ".join(words)
print(sentence)
Escape characters and raw strings

Certain characters need to be "escaped" with a backslash to be included literally in a string:

print("Line one\nLine two")   # \n — newline
print("Name:\tAlex")          # \t — tab
print("She said \"hi\"")      # \" — literal double quote inside a double-quoted string

Sometimes you want backslashes treated literally instead — file paths on Windows are the classic case. A raw string, prefixed with r, tells Python to ignore escape sequences entirely:

path = r"C:\Users\Alex\Documents"
print(path)   # C:\Users\Alex\Documents — backslashes stay as-is
Checking string properties

Strings have several built-in methods for checking what kind of characters they contain:

print("12345".isdigit())     # True
print("Hello".isalpha())     # True
print("Hello123".isalnum())  # True — letters and numbers, no spaces or symbols

print("a" in "banana")       # True — membership check

These are useful for basic input validation before trying to convert or process a string further.

Common beginner pitfalls
  • Trying to modify a string in place. As covered above, word[0] = "X" will always raise a TypeError. Build a new string instead.

  • Mismatched quotes. Opening with " and closing with ' (or vice versa) causes a syntax error — covered in the earlier article on Python syntax basics, but it's worth the reminder here since it comes up constantly once you start working heavily with strings.

Strings are one of the types you'll use in nearly every Python program you write, so getting comfortable with slicing, the core methods, and f-string formatting early pays off across everything that follows.

PREVIOUSNEXT LESSON