Video Summary1/21/2026

Strings Slicing and Operations on Strings in Python | Python Tutorial - Day #12


Python Tutorial - Day #12: Strings Slicing and Operations


1. Summary


This video by CodeWithHarry covers string slicing and basic string operations in Python, crucial concepts for beginners. The video explains how to access portions of strings using indexing and slicing, including the use of positive and negative indices. It also touches on finding the length of a string using the `len()` function. The video emphasizes the practical use of strings in various programming projects and concludes with a quiz to test understanding.


2. Key Takeaways


* **String Slicing:** Allows accessing substrings using start and end indices.

* **Indexing starts at 0:** Remember that Python (and most programming languages) uses zero-based indexing.

* **`len()` function:** Used to find the length of a string.

* **Negative Indexing:** Provides a way to slice strings from the end.

* **Understanding the [start:end] syntax**:

* `start` is the starting index (inclusive).

* `end` is the ending index (exclusive).

* If `start` is omitted, it defaults to 0.

* If `end` is omitted, it defaults to the end of the string.


3. Detailed Notes


#### 3.1 Introduction


* Focus: String slicing and operations in Python.

* Strings are a fundamental data type in programming, used frequently in projects.


#### 3.2 String Slicing Basics


* **Concept:** Accessing a portion of a string.

* **Example:** `name = "Harry"`; accessing "arr" from the string.

* **Syntax:** `string_name[start:end]`

* `start`: The index to start the slice (included).

* `end`: The index to end the slice (excluded).

* **Index Counting:** Starts from 0. Example: "Harry" : H(0), a(1), r(2), r(3), y(4)

* **Example in Repl:**

* `names = "Harry, Shubh"`

* `print(names[0:5])` -> outputs "Harry" (characters from index 0 up to, but not including, index 5).


#### 3.3 Length of a String


* **Function:** `len()` is used to determine the length of a string.

* **Example:** `name = "Harry, Shubh"`; `len(name)` returns 13.

* `len()` counts all characters, including spaces and commas.


#### 3.4 Slicing Examples


* **Example 1**: `fruit = "Mango"`

* `mangoLen = len(fruit)` -> `mangoLen` will be 5

* `print(fruit[0:4])` -> Output: "Mang" (characters at indices 0, 1, 2, 3). The 4th index, is not included.

* **Omitting the Starting Index:** `print(fruit[:4])` will produce the same output "Mang". (It defaults to 0)

* **Omitting the Ending Index:** `print(fruit[:])` or `print(fruit[0:])` Output: Mango (It defaults to the length of the string).

* **Slicing specific indices:** `print(fruit[1:4])` Output: "ang" (characters at indices 1, 2, and 3).


#### 3.5 Negative Indexing


* **Concept:** Accessing characters from the end of the string.

* **Example:** `fruit = "Mango"`.

* `print(fruit[-1])` -> Output: "o" (The last character).

* **How it works:** The interpreter adds the length of the string to the negative index.

* In the `fruit` = "Mango" example, `-1` is equivalent to `5 - 1`, which is index 4.

* `-3` is equivalent to `5 - 3` which is index 2.

* `print(fruit[-3:])` -> Output: "ngo"

* `print(fruit[-3:-1])` -> Output: "ng". It's [2:4], not including index 4.

* `print(fruit[-3:len(fruit)-1])` is similar to the previous command.

* `print(fruit[-3:-1])`

* **Important Point:** `[0:4]` means including 0, but not including 4. It is the same with 1 as well. The same rule is applied to the negative indices.


#### 3.6 Quiz


* **Problem:** Determine the output of `nm = "Harry"; print(nm[-4:-2])`

* **Action:** Solve the quiz and tag the instructor on Instagram with the correct answer.


Why this video matters

This video provides valuable insights into the topic. Our AI summary attempts to capture the core message, but for the full nuance and context, we highly recommend watching the original video from the creator.

Disclaimer: This content is an AI-generated summary of a public YouTube video. The views and opinions expressed in the original video belong to the content creator. YouTube Note is not affiliated with the video creator or YouTube.

This summary was generated by AI. Generate your own unique summary now.