[Python Introduction 08] Strings: From “Storing Text” to “Precise Slicing”, This Article Covers Basic Operations Thoroughly

When developing in Python, we deal with “text” every day—user input nicknames, fields returned by APIs, error messages in logs… These text data are stored and manipulated in Python using strings (str).

Many beginners think that “strings are simple, just writing some text”, but actually, “how to precisely find a character” and “how to extract the desired segment” are the key basic operations for subsequent processing of complex text. Today, we will start with “defining strings”, first understanding its “index logic”, and then mastering the “slicing skills”, allowing you to easily grasp the basics of strings.

1. First Understand: What is a String? How to Define It?

A string is the most basic “text container” in Python, specifically used to hold “characters”—whether they are letters, numbers, or symbols, as long as they are enclosed in quotes, they are strings.

1. Defining a String: Use Two Types of Quotes

Defining a string is super simple, just wrap the text with single quotes or double quotes, both methods are completely equivalent.

First, let’s look at the difference from numeric types to help you quickly distinguish between “numeric” and “text”:

# Numeric type: no quotes, stores a value (can be calculated directly)<span>age = 25<br />print(type(age))<br /># Output: <class 'int'> (integer type)</span># String type: wrapped in quotes, stores text (cannot be calculated directly)username = “Zhang San”
# Double quotes wrap Chinese
phone = ‘13812345678’
# Single quotes wrap numbers (at this time it is text, not a value)
print(type(username))
# Output: <class ‘str’> (string type)
print(type(phone))
# Output: <class ‘str’> (string type)

Special Case: What if the Text Contains Quotes?

If the text itself contains quotes (like dialogue, English quotes), using “different types of quotes nested” can avoid errors:

Text contains single quote → Outer layer uses double quotes<span>dialog = "Xiao Ming said: 'I am learning Python'"<br /># Text contains double quotes → Outer layer uses single quotes<br />book = 'Recommended reading "Python Programming: From Beginner to Practice"'<br />print(dialog)<br /># Output: Xiao Ming said: 'I am learning Python'<br />print(book)<br /># Output: Recommended reading "Python Programming: From Beginner to Practice"</span>

2. The “Secret” of Strings: Each Character Has a “Number”

Unlike numbers, strings are “ordered containers”—each character occupies a separate “position”, and Python assigns a “number” to each position, which is called index.

For example, the string <span>s = "abcdef"</span> has the following storage and index correspondence in memory:

From Left (Positive Index) 0 1 2 3 4 5
Character a b c d e f
From Right (Negative Index) -6 -5 -4 -3 -2 -1

3 Essential Rules to Remember

  1. Positive index starts from 0: The first character is 0, the second is 1, and so on;
  2. Negative index starts from -1: The last character is -1, the second to last is -2, suitable for quickly accessing the end content;
  3. Cannot go out of bounds: The string length is 6 (index 0~5), if you forcefully use <span>s[6]</span>, it will directly throw an error (<span>IndexError</span>).

3. Using Index: Precisely “Extract” a Single Character

Knowing the index allows you to directly access a character at a specific position like “finding a seat by number”. The format is <span>string[index]</span>.

s = “abcdef”
# Positive index: from left
print(s[0])
# Output: a (first character)
print(s[3])
# Output: d (fourth character)
# Negative index: from right (super useful!)
print(s[-1])
# Output: f (last character)
print(s[-4])
# Output: c (fourth to last character)

Practical Example: Extracting the Last 4 Digits of a Phone Number

No complicated operations needed, just use negative index to directly take the last 4 characters (feel it first, slicing will be detailed later):

phone = “13812345678”
last4 = phone[-4:]
# Get the last 4 characters
print(“Last 4 digits of phone number:”, last4)
# Output: Last 4 digits of phone number: 5678

2. Slicing: The “Scalpel” of Strings, Precisely Extracting Text

If “index” is for taking a single character, then “slicing” is for taking a segment of continuous characters—for example, extracting “05” from “2024-05-20”, or “World” from “Hello World”.

Slicing can be used not only on strings but also on lists and tuples; it is a “universal skill” in Python that must be mastered.

1. Slicing Syntax: Remember <span>[start:end:step]</span>

The core format of slicing consists of these 6 characters, with three parameters controlling “start point”, “end point”, and “step”, all parameters can be omitted (using default values).

Parameter Function Default Value Reminder
start Starting position of the slice (includes this position) 0 (the leftmost) Can be negative (e.g., start=-3, starting from the third to last)
end Ending position of the slice (not included!) String length Can be negative (e.g., end=-2, ending at the second to last)
step Step (the interval for taking characters) 1 (take continuously) Positive numbers go from left to right, negative numbers go from right to left (to achieve reversal)

⚠️ The Most Common Pitfall: <span>end</span> is “not included”! For example, <span>s[0:3]</span> takes indices 0, 1, 2, not 0, 1, 2, 3.

2. 12 Examples: Covering 90% of Slicing Scenarios

We will use <span>s = "abcdef"</span> to demonstrate, with each example paired with practical uses, so you will know how to use them after reading.

(1) Basic Operation: Taking a Middle Segment

# Take indices 0~2 (not including 3) → abc<span>print(s[0:3])<br /># Output: abc<br /># start omitted (default 0) → equivalent to s[0:3]<br />print(s[:3])<br /># Output: abc<br /># end omitted (default to the end) → take from index 3 to the end → def<br />print(s[3:])<br /># Output: def</span>

(2) Using Negative Index: Quickly Taking Both Ends

# Take from index 1 to the last one (not including -1) → bcde<span>print(s[1:-1])<br /># Output: bcde<br /># Take the last 3 characters → def<br />print(s[-3:])<br /># Output: def<br /># Take the first 3 characters → abc (negative index can also be used as end)</span>

(3) Step: Control the “Interval of Character Extraction”

# Step=2: Take one every other character → ace (indices 0, 2, 4)
print(s[::2])
# Output: ace
# Step=3: Take one every two characters → ad (indices 0, 3)
print(s[::3])
# Output: ad
# Starting from index 1, step=2 → bd (indices 1, 3)
print(s[1:5:2])
# Output: bd

(4) Reverse Slicing: Achieving “String Reversal”

Setting the step to a negative number means taking from right to left, the most common scenario is “reversing a string”:

# Step=-1: Take all from right to left → fedcba (one line reversal!)
print(s[::-1])
# Output: fedcba
# Step=-2: Take one every other character from right to left → fdb (indices 5, 3, 1)
print(s[::-2])
# Output: fdb
# From index 5 to 1 (not including 1), step=-1 → fedc (indices 5, 4, 3, 2)
print(s[5:1:-1])
# Output: fedc

3. Slicing in Practice: Solving Real Problems

Practice 1: Extracting Year, Month, Day from a Date

Given the date string <span>date = "2024-05-20"</span>, use slicing to extract year, month, and day:

date = “2024-05-20”
year = date[:4]
# First 4 characters → 2024
month = date[5:7]
# Indices 5~6 → 05 (note that the “-” in between occupies index 4)
day = date[-2:]
# Last 2 characters → 20
print(f”Year: {year}, Month: {month}, Day: {day}”)
# Output: Year: 2024, Month: 05, Day: 20

Practice 2: Validating Email Format (Extracting Username Before @)

Check if the email contains “@” and extract the username:

email = “[email protected]
# First check if there is @
if “@” in email:
username = email[:email.find(“@”)]
# find() finds the index of @, which will be explained later
print(“Username:”, username)
# Output: Username: zhangsan
else:
print(“Invalid email format”)

3. After-Class Exercises: Use Slicing to Solve Real Problems

After learning slicing, let’s do a small exercise to consolidate the knowledge points.

Exercise Requirements

Given the string <span>s = "Hello World!"</span>, complete the following operations:

  1. Print the character at index 4;
  2. Print all characters of the string;
  3. From index 1, print all subsequent characters;
  4. From the starting position, take up to the character before index 5;
  5. From the starting position, take up to the last character (not including);
  6. From the fourth to last character, take up to the last character (not including);
  7. From index 1, take up to the character before index 5, with a step of 2;
  8. Reverse the string.

Reference Code and Results

s = “Hello World!”
# 1. Character at index 4 → o
print(s[4])
# Output: o
# 2. All characters → Hello World!
print(s[:])
# Output: Hello World!
# 3. Starting from index 1 → ello World!
print(s[1:])
# Output: ello World!
# 4. Up to index 5 → Hello
print(s[:5])
# Output: Hello
# 5. Up to the last character → Hello World
print(s[:-1])
# Output: Hello World
# 6. From the fourth to last up to the last (not including) → rld
print(s[-4:-1])
# Output: rld
# 7. From index 1 to before 5, step 2 → el
print(s[1:5:2])
# Output: el
# 8. Reverse → !dlroW olleH
print(s[::-1])
# Output: !dlroW olleH

<span>4. Summary: The 2 Core Concepts of String Basics</span>

  1. Understand Index Logic: Remember “positive index starts from 0, negative index starts from -1”, which allows for quick location of single characters, especially the use of negative index to access end content, which is very common in practice;
  2. Master Slicing Rules: The core is <span>[start:end:step]</span>, with a focus on remembering that “end is not included” and “step controls direction”. Through more practice (like extracting dates, reversing strings), you can master it proficiently.

Strings are the “basic container” of Python, and many logics (like slicing) are common when learning lists, dictionaries, and other containers later. Mastering these basic operations lays a solid foundation for future learning.

Leave a Comment