Python String Slicing Techniques

🔷 Python String Slicing String slicing is used to extract a part of a string. Syntax: 👉 string[start : end] ➡ End index is not included 🔹 1️⃣ Basic Slicing ▶ Example txt = "I have a toy" print(txt[2:6]) ✔ Output: have ➡ Starts from index 2 and ends at 5 🔹 2️⃣ From Beginning (Default Start Index) ▶ Example print(txt[:6]) ✔ Output: I have ➡ Starts from index 0 🔹 3️⃣ Till the End (Default End Index) ▶ Example print(txt[9:]) ✔ Output: toy ➡ Goes till the last character 🔹 4️⃣ Negative Indexing in Slicing Negative indexing starts from the end. ▶ Example print(txt[-2]) ✔ Output: o ➡ Second character from the end 🔹 5️⃣ Slicing with Negative Index ▶ Example print(txt[-10:-4]) ✔ Output: have a ➡ Works from the back of the string 🔹 6️⃣ Another Example ▶ Example value = "welcome" print(value[3:5]) ✔ Output: co 📌 String slicing is very important in Data Analytics and text processing. #Python #StringSlicing #LearningPython #CodingJourney #DataAnalytics

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories