Master the power of text manipulation in Python. Learn string slicing, negative indexing, immutability, built-in string methods, and f-strings to build real-world text-processing applications.
Strings are one of the most commonly used data types in Python. Whenever we work with text such as names, messages, emails, passwords, addresses, or user input, we use strings.
A string is simply a sequence of characters enclosed within quotes. Strings allow us to store, display, modify, and manipulate textual data easily.
You can define strings using single, double, or triple quotes. Choose the one that suits your needs.
Use for short strings where you don't need double quote escaping.
The standard style for most Python developers.
Perfect for writing multi-line text or preserving line breaks.
Every character in a string occupies a position number called an index. Python starts counting indices from 0.
To access a specific character, write the variable name followed by the index enclosed in square brackets `[]`.
Slicing allows us to extract a portion of a string. We specify where to start and where to end.
If you omit the start, it defaults to `0`. If you omit the end, it defaults to the length of the string.
Once a string is created in memory, its individual characters cannot be changed or modified in place.
If you want to modify a string, you must create a new string variable with the desired change. Python does this to optimize memory and run faster.
Python provides powerful built-in functions and object methods to analyze, validate, and manipulate text strings.
Escape sequences start with a backslash `\` and are used to include characters that are otherwise hard or impossible to write inside strings.
Almost every Python application deals with string data. Here are the primary domains:
Authenticating usernames, matching encrypted password hashes, and processing logins.
Formatting and validating emails, scanning messages for spam, and routing alerts.
Parsing human questions, generating text responses, and feeding text into models.
Analyzing web text queries, matching strings using indexing, and ranking results.
'...'), double quotes ("..."), or triple quotes ('''...''' or """..."""). It is an immutable data type, meaning once a string is created, its individual characters cannot be modified or replaced.name[0]). String Slicing is used to extract a range of characters (a substring) using the start and end index boundaries (e.g. name[1:4]). Slicing doesn't raise an error even if indices are out of range.word[0] = 'P' will raise a TypeError. If you need a modified string, you must create a new string variable. This ensures memory efficiency and security.find(sub) method searches for a substring and returns the lowest index where it begins (or returns -1 if it is not found). The replace(old, new) method returns a new string where all occurrences of the old substring are replaced by the new substring. Neither method alters the original string.\) followed by a letter or symbol. They are used to format text in ways that are otherwise hard to express directly. Common escape sequences include \n for newline, \t for a horizontal tab space, and \" or \' to insert literal quotes inside single or double-quoted strings.len() (gets string length), endswith(suffix) (checks suffix), count(substring) (counts occurrences), capitalize() (capitalizes the first character), upper() / lower() (case conversion), and strip() (removes leading/trailing whitespace).Write every line yourself — the best way to learn is by doing, not just reading.
Write a Python program to display a user-entered name followed by "Good Afternoon" using the input() function.
input() to get the name, and print using an f-string: f"Good Afternoon, {name}".Write a program to fill in a letter template dynamically, replacing <|Name|> and <|Date|> placeholders with actual inputs.
.replace("<|Name|>", name).replace("<|Date|>", date).Write a Python program to detect double spaces (e.g. " ") inside a target string.
.find(" ") method. A return value other than -1 indicates double spaces exist.Modify the program from Question 3 to replace all double spaces in a string with a single space.
.replace(" ", " ") method. Note that because strings are immutable, this returns a new string.Format the following letter using escape sequence characters for clean readability: "Dear Harsh, this Python course is amazing. Thanks!"
\n and tab \t escape characters inside a single string.'...'), double ("..."), or triple ('''...''') quotes.0 for the first character, while Negative Indexing starts at -1 from the right side.string[start:end] (non-inclusive of the end index).string[start:end:skip] retrieves characters by stepping over the interval defined by skip.len(), capitalize(), endswith(), count(), find(), and replace()..upper(), .lower(), and leading/trailing whitespace can be trimmed with .strip().\n for newline, \t for tab) let you include special characters inside text.You have successfully completed Chapter 3 — Strings. You can now process, clean, and format textual data in Python!
Three chapters down, ten to go. You're building serious Python foundations — keep the momentum!