Extracting Parts of Strings in Python: How to Use Indexing and Slicing Syntax

When handling strings (str type) in Python, operations such as retrieving a specific single character or extracting a substring from a certain range are frequently performed.

We use “Indexing” and “Slicing Syntax” for these operations. This article explains the basic methods for extracting necessary data from strings.

目次

1. Getting a Single Character with Indexing

Strings are treated like “arrays of characters.” By writing [Index Number] after the string variable, you can retrieve the specific character at that position.

Important Rule: The index starts from 0 (the first character is the 0th).

Specific Usage Example: Parsing Product Codes

This example assumes a case where the first character of a product code represents a category.

# Product code
product_code = "A-5091"

# Get the first character (0th)
category_char = product_code[0]

# Get the second character (1st)
hyphen_char = product_code[1]

print(f"Product Code: {product_code}")
print(f"Category: {category_char}")
print(f"Separator: {hyphen_char}")

Execution Result:

Product Code: A-5091
Category: A
Separator: -

Negative Indexes (Access from the End)

If you specify a negative number for the index, you can specify the position from the end of the string. [-1] points to the last character, and [-2] points to the second to last character.

# Get the last character (e.g., check digit)
last_char = product_code[-1]

print(f"Last Character: {last_char}")

Execution Result:

Last Character: 1

2. Getting Substrings with Slicing Syntax

To extract a specific range from a string, such as “from the 3rd character to the 5th character,” use slicing syntax.

Syntax:

string[start_index : end_index]

Important Notes:

  • Start Index: Included in the range.
  • End Index: Not included in the range (retrieves up to the character before it).

Specific Usage Example: Splitting Date and Time Strings

Here is an example of extracting the date part and time part from a fixed-format datetime string.

# Date and time data (YYYY-MM-DD HH:MM)
timestamp = "2025-12-05 14:30"
# Index: 0123456789...

# Date part (from 0th up to before 10th)
date_part = timestamp[0:10]

# Time part (from 11th up to before 16th)
time_part = timestamp[11:16]

print(f"Full: {timestamp}")
print(f"Date: {date_part}")
print(f"Time: {time_part}")

Execution Result:

Full: 2025-12-05 14:30
Date: 2025-12-05
Time: 14:30

Omitting Indexes

In slicing syntax, you can omit the start or end index.

  • [start :]: From start position to the end.
  • [: end]: From the beginning up to before the end position.
  • [:]: Copy the whole string.
# From 11th to the end (Time part)
time_only = timestamp[11:]

# From beginning up to before 4th (Year part)
year_only = timestamp[:4]

print(f"Time (Short notation): {time_only}")
print(f"Year (Short notation): {year_only}")

Execution Result:

Time (Short notation): 14:30
Year (Short notation): 2025

Summary

  • text[i]: Retrieves the character at index i (starts from 0).
  • text[-1]: Retrieves the last character.
  • text[start:end]: Retrieves the string from start up to before end.
  • Be careful that the end index is “not included.”
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次