Academy
ProgrammingPython for BeginnersStrings in Depth

Strings in Depth

Strings Are Everywhere

Almost every real program deals with text. Names, messages, addresses, search queries, file contents — all of it is strings. Python's first-class string support is one of the reasons it became so dominant.

You already know how to create strings and print them. This lesson goes deeper — the tools Python gives you to inspect, transform, and format text.

Strings Are Sequences

A string is a sequence of characters, just like a list is a sequence of values. Everything you learned about indexing and slicing for lists applies to strings too.

city[0] is "K". city[-1] is "a". city[1:4] is "olk". The same rules, applied to characters instead of list items.

Strings Are Immutable

Here is one important difference from lists: strings cannot be changed after they are created.

This crashes with a TypeError. You cannot reach into a string and replace a character the way you can with a list.

If you need a modified version of a string, you create a new one:

name = "Priya"
name = "B" + name[1:]   # "Briya"

Python builds the new string and points the variable at it. The original "Priya" is discarded. The label name moves to the new string.

Immutability might feel limiting, but it makes strings safe and predictable — a string you pass to a function can never be modified without your knowledge.

String Methods

Python gives strings a rich set of built-in methods — functions attached to the string itself, called with dot notation.

Changing case

None of these modify message — remember, immutable. They each return a new string with the transformation applied.

Removing whitespace

Whitespace stripping is one of the most common operations when handling user input — people often accidentally include leading or trailing spaces.

Checking content

These all return True or False, making them perfect to use inside if conditions.

Replacing and splitting

replace() returns a new string with every occurrence swapped. split() breaks a string at a separator and returns a list of the pieces. This is how real programs parse structured text — CSV files, API responses, user input.

The reverse of split() is join():

" ".join(words) takes a list and glues the items together with the string as separator. The separator goes on the left, the list goes inside.

f-Strings — The Clean Way to Format Text

A recurring need: embedding variable values inside a string. You've been using print("Hello,", name) — which works but has limitations. Python's modern solution is the f-string.

Place an f before the opening quote and put variable names (or any expression) inside {}:

Clean, readable, and exactly one line. No concatenation, no commas, no type conversion needed.

The {} can contain any expression — not just variable names:

The :.2f inside the second {} is a format specifier — it tells Python to display the float with exactly 2 decimal places. You don't need to memorise all format specifiers now, but :.2f for currency and decimal display is immediately useful.

Finding a Substring

find() returns the index of the first occurrence of a substring. If not found, it returns -1.

find() located @ at position 4. Everything after it is the domain. This kind of text extraction is fundamental to parsing real-world data.

A Practical Example

Three string methods chained together, then a split, then an f-string. Each method returns a new string, so you can chain them in sequence. This is idiomatic Python — small, composable operations combined to transform data.

What You Have Learned

Strings have more power than they first appear.

The key ideas:

  • Strings support indexing, slicing, and len() — just like lists
  • Strings are immutable — you create a new string rather than modifying the original
  • String methods return new strings: upper(), lower(), strip(), replace(), split()
  • split() converts a string into a list; join() converts a list back into a string
  • f-strings (f"...{variable}...") are the cleanest way to embed values in text
  • find() locates a substring; returns -1 if not found

In the final lesson of this beginner series, you'll learn about dictionaries — Python's way of storing pairs of related information, like a word and its meaning, or a name and a phone number.