Skip to main content

Command Palette

Search for a command to run...

Strings in Python (Story of Necklace).

Published
3 min readView as Markdown

Think of Python String as a necklace where each character is a bead. You can observe, copy, or rearrange parts of it — but you can’t just swap a bead in place.

Strings are beautiful, delicate , and most importantly — immutable.

What is Strings in Python ??

A String is a sequence of characters wrapped in quotes.

text = "Python is fun"

Think of “Python is fun” as a “necklace of characters”.

Index:   0 1 2 3 4 5 6 7 8 9 10 11 12
Char :   P y t h o n   i s   f  u  n

String Slicing : Cutting Your Necklace.

Syntax :-

string[start:stop:step]
SliceWhat It DoesExampleOutput
s[0:5]Characters from index 0 to 4"Python"[0:5]"Pytho"
s[:6]From start to index 5"Python"[:6]"Python"
s[2:]From index 2 to end"Python"[2:]"thon"
s[-1]Last character"Python"[-1]"n"
s[::-1]Reversed string"Python"[::-1]"nohtyP"

“Slicing is like cutting a portion of the necklace or reversing it by flipping it around.”

Immutable Nature of Strings

Python Strings are Immutable means you can ‘t change them after creation.

s = "hello"
s[0] = "H"  # ❌ Error!

“You can’ t just replace one bead in a sealed necklace. You have to make a new necklace.“

s = "hello"
s = "H" + s[1:]  # ✅ New string

Commonly Used String Methods.

This is the go-to toolkit of strings methods, with examples and meanings.

MethodPurposeExampleOutput
.lower()Convert to lowercase"HELLO".lower()"hello"
.upper()Convert to uppercase"hello".upper()"HELLO"
.title()Capitalize first letter of each word"python is fun".title()"Python Is Fun"
.strip()Remove whitespace" text ".strip()"text"
.replace(a, b)Replace substring"hi buddy".replace("buddy","Mandeep")"hi Mandeep"
.find()Find index of first occurrence"python".find("t")2
.count()Count occurrences of substring"banana".count("a")3
.split()Split into list"a,b,c".split(",")['a', 'b', 'c']
.join()Join list into string",".join(['a','b'])"a,b"
.startswith()Check if starts with"code".startswith("c")True
.endswith()Check if ends with"code".endswith("e")True
.isalnum()Checks if all chars are alphanumeric"abc123".isalnum()True
.isalpha()Checks if all are letters"abc".isalpha()True

Conclusion : Strings are Beads with Rules.

“A String might look like simple text , but in Python, it’s a structured , immutable chain of characters. You can slice it , clone it , analyze it , but never mutate it in-place.”

Thank you , once again to read the article till here , and always open for any suggestions/criticism.

More from this blog