Automatically translated.View original post

🔪 Delve into Python Strings: Super Bang Slicing! 🌟

Let's go to Basic Python on Strings! Today we're going to see the technique of cutting text, also called Slicing! It's very simple and useful.

Slicing uses the [start: end] sign to select the part of the String we want:

1.Basic Slicing: Print (b [2: 5])

• It will run from index 2 to before index 5 (index 5 will not be included!)

2.Slice From the Start: Print (b [: 5])

• If we leave the initial index, it starts to cut the first letter.

3.Slice To The End: Print (b [2:])

• If we leave the final index, it will cut from the given index to the last letter of the String.

4.Negative Indexing: Print (b [-5: -2])

• Use negative numbers to start counting from the end of the string! This example draws the character from the 5th position, from the end (-5) to the second position, from the end (-2).

Try to use this technique to handle text messages. It is a lot more convenient! Practice often and will be better by yourself. Fight! 💪💖

# python # data # ai # programmer # learnonlemon8

2025/11/10 Edited to

... Read morePython String Slicing เป็นฟีเจอร์ที่สำคัญและจำเป็นมากสำหรับการเขียนโปรแกรมด้วยภาษา Python โดยเฉพาะกับการจัดการข้อความ ซึ่งการเข้าใจคอนเซ็ปต์นี้จะช่วยให้โค้ดของเรากระชับและง่ายต่อการอ่านมากขึ้น โดยเทคนิค Slicing จะใช้รูปแบบตัวอย่างเช่น b="Hello, World!" และสั่งประมวลผลด้วยคำสั่ง print(b[start:end]) เพื่อเลือกส่วนของข้อความในสตริงตามช่วงที่กำหนด ถือเป็นการดึง substring ออกมา ใช้ได้อเนกประสงค์มาก ไม่ว่าจะตัดคำ สกัดข้อมูล หรือแม้แต่การเปลี่ยนแปลงบางส่วนของข้อความ ส่วนที่น่าสนใจอีกอย่างคือ Negative Indexing ซึ่งช่วยให้เราสามารถนับตำแหน่งตัวอักษรจากด้านท้ายของสตริงได้ เช่น print(b[-5:-2]) จะดึงข้อความตั้งแต่ตำแหน่งที่ 5 นับจากท้าย ไปจนถึงก่อนตำแหน่งที่ 2 นับจากท้าย เหมาะมากกับกรณีที่ต้องการตัดข้อความจากท้ายไฟล์หรือตัวอักษรท้าย ๆ โดยไม่ต้องคำนวณความยาวสตริงด้วยตัวเอง เทคนิคนี้ยังมีประโยชน์มากในการทำงานกับข้อมูลในรูปแบบสตริง เช่น การแยกคำจากประโยค การดึงเฉพาะส่วนของข้อมูลที่สนใจจากข้อความขนาดใหญ่ อย่างเช่น ดึงชื่อหรือแยกเบอร์โทรศัพท์จากข้อความที่ปนกันอยู่ นอกจากนี้การเข้าใจ Slicing ยังเป็นพื้นฐานที่ดีในการเรียนรู้ขั้นสูงเกี่ยวกับ Python เช่น การสร้าง List Slicing หรือ Manipulation ของ Data Structures อื่น ๆ ที่ใช้รูปแบบเดียวกัน ทำให้ทักษะการเขียนโค้ดพัฒนายิ่งขึ้น ลองฝึกเขียนโค้ดและทดลองใช้ Python String Slicing ในโปรเจกต์เล็ก ๆ หรือเริ่มจากตัวอย่างง่าย ๆ ที่สอนในบทความ เพื่อให้คุ้นเคยกับการใช้ start, end index รวมถึง Negative Indexing รับรองว่าเมื่อต่อไปต้องแก้ไขข้อความหรือข้อมูลจะสะดวกรวดเร็วและลดข้อผิดพลาดลงมากแน่นอนค่ะ อย่าลืมลองเล่นกับข้อความ "Hello, World!" เหมือนตัวอย่างในบทความเพื่อเข้าใจการทำงานโดยตรง และสามารถพัฒนาทักษะต่อยอดไปกับการเขียนโปรแกรมสาย Data Science, AI หรือโปรแกรมเมอร์ที่ต้องจัดการข้อมูลจำนวนมากได้อย่างมีประสิทธิภาพ

Related posts

My Programming Interest Just Skyrocketed! 🚀
I just discovered something that has made my interest in programming explode to 1000000000000000%! 🤯 🎮 Learning Programming is Like a Game Adventure! From Python, Java, JavaScript, to HTML, there are all kinds of programming languages available, along with systematic topic courses. The learning p
Valder

Valder

2141 likes

Books that left my jaw on the floor
Fair warning, friends: these books are NOT for the faint of heart. They're raw, intense, and full of messy emotions. They delve into darkness, pain, and the messy realities of life. But they also offer glimpses of hope, resilience, and the incredible strength of the human spirit. I've fe
Meredith Jao

Meredith Jao

9734 likes

8 Free Online Certifications to Boost Your Resume
Hi, lemons! In today's competitive job market, having the right certifications can make a significant difference in showcasing your skills and expertise. Online certifications are not only convenient but also cost-effective. Here are six free online certifications that can give your resume a va
Lifestyle Babe

Lifestyle Babe

1214 likes

Shadow Work Journaling Prompts ✍🏻
Shadow work involves delving into the unconscious aspects of oneself, known as the "shadow," which encompasses hidden emotions, desires, fears, and traits often deemed unacceptable or repressed. It's a psychological process aimed at acknowledging, understanding, and integrating these da
mindofieva

mindofieva

493 likes

How to become an academic weapon in 3 steps 🩷
Hey fellow students of Lemon8! I wanted to share a great new study technique I learned, called the three pass method. I’ve used this as a great way to work through long amounts of content, and organize my thoughts when learning new things. The first pass is simply for recognition, get yo
bea_studies_PA

bea_studies_PA

59 likes

YesStyle Korean Skincare Haul
I’ve heard endless good things about Korean skincare products, so I decided it was finally time to delve into something new. I’ll update with results! Products and prices are listed in the pictures. I ordered everything off of YesStyle, as I heard that was the best place to buy from. Although, it d
Kayley🌸

Kayley🌸

327 likes

✝️💕Bible Study Essentials💕✝️
These are my bible study essentials I use to delve deeper into the word! 1. Bible Study Apps 📱 I love using bible study apps because I can access reading plans, topic deep-dives, chapter summaries, verse-by-verse commentaries and different bible translations for a deeper understanding of the W
Denise💕

Denise💕

141 likes

Trash or Treasure? Fruit Peels!
Did you know that fruit peels pack a nutritional punch even greater than the fruit flesh itself? Do you typically peel your fruits before indulging? Many opt to discard the peel due to its texture or concerns about pesticide residue. However, you might be surprised to learn that these seemingly
Elisa L.

Elisa L.

32 likes

Vanilla cupcake-For the gourmand girlies🧁🍨🤎
✨Eclaire + Cream Velvet✨ #gourmandfragrance #vanillaperfume #fragranceaddict #scentlayeringcombos #layeringperfume
Navy♡︎ SAHM Scent Diaries✨

Navy♡︎ SAHM Scent Diaries✨

50 likes

Unseen Perks of Being a Transfer Student
🌆 1. Discover Your Ideal College Experience Your time at community college isn't just a stepping stone; it's a period of self-discovery. Do you see yourself thriving in a bustling city environment, or do you prefer the charm of a college town? Maybe it’s the allure of sunny weather or the
Lizz

Lizz

120 likes

🧀🍗 Parmesan Chicken Meatloaf 🍗🧀 Savor the delicious flavors of this Parmesan Chicken Meatloaf, a unique twist on a classic dish, featuring ground chicken, Parmesan cheese, and a blend of Italian seasonings. Perfect for a comforting family dinner! 📋 Ingredients: - 2 tbsp olive oil - ⅓ cup on
👑Jonquil Lewis

👑Jonquil Lewis

11 likes

BL Omegaverse with a Surprising Green Flag
📖 Name of the Book: The Origin of Species on Tappytoon 📄 Plot: Lee Youngjin is an omega that is very sickly in a world where being an omega or alpha is extraordinary and special. But the only thing Youngjin thinks is special about himself is his skills as a hacker. After a heist goes haywire,
Cozywulfe | BL Reading Diary

Cozywulfe | BL Reading Diary

311 likes

Quotes i live by as a college student ⭐️📣
There are many times throughout college that I think back on quotes.m to help me gain motivation. I soon realized I didn’t have very many quotes that inspire me anymore, so I found some on BeFreed. BeFreed is the #1 smart reading app that condenses new nonfiction into: • 40 min deep dives
Naomilily

Naomilily

4 likes

LEARN OVER 300 BUSINESSES FOR $19
RBA sale is coming to an end don’t miss out! Comment “ LEARN “ so you can join now and start learning how to earn new streams of passive income. 💰 #passiveincomeonline #workforyourself #startanonlinebusiness #dfydigitalproducts #rba #dba #swc #workfromhomemom #sidehustle #donefory
Momsmakingmoney

Momsmakingmoney

9 likes

ADHD: what you need to know!!
I, personally, am going through this phase right now to where I feel like every one I care about is turning away from me. That is why I decided to share this content for anyone that might be feeling the same… <3 ADHD’ers, UNITE! #rsdadhd #rsd
*ೃ༄Lera Batt *ೃ༄

*ೃ༄Lera Batt *ೃ༄

337 likes

✨ Book Discouragement 📚
Let me read a crappy book so you don't have to! Triggers: SA of a minor, untreated mental health issues, violence, murder, manipulation, mental/emotional abuse of a minor, explicit scenes & descriptions in excessive use.  ✨ First and foremost, I just want to say that I do not support
rhianon marie

rhianon marie

328 likes

👀🪴The Science ofWhy Plants BoostYour Mood🪴👀
🪴Incorporating more plant life into your home and routine can dramatically enhance your quality of life. 🪴From improving air quality to boosting your mood, the numerous benefits of plants are backed by science and are accessible to anyone willing to engage with nature. 🪴So, gather your pots, soil
Nature Elegance

Nature Elegance

36 likes

Devotionals to delve into!🙏🏾
If you are looking to study the Bible and need some inspiration, here is some! After I complete a Bible Study, I write the title and scripture so you can reference what was taught. I teach every Wednesday so you can see the recording on Facebook as well. Abundance & Blessings 💕🥰🙏🏾
Lady Nini 💕

Lady Nini 💕

16 likes

Releasing What No Longer Serves You
🦋In the journey of life, we often find ourselves weighed down by negative emotions, past traumas, and limiting beliefs that hinder our growth and prevent us from experiencing true happiness. Learning the art of letting go is not only a transformative process that allows us to release what no longer
𓇢𓆸k e n z

𓇢𓆸k e n z

55 likes

Helaina Sharpley’s Wire Art☕️
Artist Helaina Sharpley creates works that blur the lines between drawing and sculpture. Inspired by the rituals and etiquette of tea drinking from the past, her early pieces focused on porcelain cups and Victorian architecture. Her recent explorations delve into rural landscapes and British flora,
Hues Clues

Hues Clues

11 likes

Ready to deepen your relationship and prayer life?
I am actively on the lookout for women who are eager to embark on a transformative journey through "Fervent," a powerful book that guides us in praying strategically. As I seek individuals ready to delve into the depths of prayer with intention and purpose, I invite you to join me on this e
Prospherr

Prospherr

7 likes

A Relaxing Routine for Peaceful Nights
"Unwind and relax with me as we delve into the art of creating the perfect evening routine in today's video! 🌙✨ Discover the secrets to winding down with calming activities, disconnecting from screens, and prioritizing rituals that promote restful sleep. Join me on a journey to cultivate a
DailyInspirewis

DailyInspirewis

3 likes

Freshmen ✨Brainrot✨ to IVY LEAGUE vibes 🌸🤌💋🤧🌷📸
Yes, they’re free-ish and yes, they’ll make you feel smart 😏 📑📚 These apps helped me📚📑 1. Obsidian 🚂 – For serious note-takers who love linking ideas together like a mind map. 2. Taskheat 🔗– Turns your messy to-do list into a visual flowchart. 3. Bearable 🐾– Track mood, energy, habits, and s
ang3lie

ang3lie

1 like

october tbr 🍂🪦🤎👻
do we think i can make it through all of these this month?😅 #booktok #bookstagram #readersoflemon8 #october #tbr
✰ ꜱᴋʏᴇ ☾

✰ ꜱᴋʏᴇ ☾

32 likes

Reconnecting Through Self-Care
Reconnecting Through Self-Care: The Power of Unplugging" Description: "Join me as we explore the rejuvenating practice of unplugging to reconnect with ourselves! 🌿 In this video, I delve into the importance of stepping away from screens and distractions to embrace self-care and tranquility
DailyInspirewis

DailyInspirewis

8 likes

Quotes that got me through high school 🌸🍋
There’s always a new quote somewhere waiting to be found. I believe everyone has, like soulmates, a quote that belongs to them. Here’s the app where I found mine: BeFreed condenses new release nonfiction into: • 40 min deep dives • 20 min podcast (Change host voice or tone to your liking) •
Naomilily

Naomilily

1 like

ANTHOLOGY PART 13
BE WHERE YOU ARE (Psalms 139:23‭-‬24 inspired) Oh, God! Lead me away from myself, for my heart is wicked and dark. Draw me in with your light. Light up my path, so I can find my way to you. Lord, I know you're near ❤ Lord, I crave your wisdom. I need your truth. I long for your con
Sarah Hilde

Sarah Hilde

95 likes

Bible Notes 📖 | Psalms 38-42
Bible notes 📖 | Psalms 38–42 These chapters are raw. They’re honest. They’re full of pain, repentance, and hope. Psalm 38 begins in brokenness, and Psalm 42 ends in holy longing. 💔 When David felt the weight of sin, he cried out. 🕊️ When he felt forgotten, he clung to God’s presence. ❤️‍🔥 And
jazz_whit

jazz_whit

216 likes

Hidden Signs of ADHD: Insights for Women
Hello honey ❤️ Living with ADHD is like navigating through a maze of surprises, and sometimes, the symptoms manifest in unexpected ways, especially for women. Let’s delve into some lesser-known ADHD symptoms that you might not expect but are more common than you think. Here’s the lowdown: 📌
Alena Artemenko

Alena Artemenko

105 likes

My Pros and Cons Of Working In Vet Med
I understand that my passion drives me to focus on the positive aspects of things, but I also acknowledge that it's hard to ignore the negatives at times. Although I won't delve into it now, I do want to discuss the reality of compassion fatigue in a future post. However, what I want you to
ThatVetMedGirl

ThatVetMedGirl

198 likes

Steven king
I’ve had this book on my TBR showed it a few times but now I’m finally able to get into it. I had to take a break from my other book “Haunting of Aderline” so I read a short story and now gonna start this and go back and forth between my two reads. #horrorbooks #stevenking #horror #bookworms
Officalcozyhome

Officalcozyhome

24 likes

Rotel dip
Believe it or not rotelle dip is a healthy choice of eating Rotel dip Rotel(any kind of your choice) Ground beef 1 pound Any sausage of your choice 1/2 pound Velveeta cheese Queso cheese Green onion Parsley 1/2 cream cheese Chips of your choice #rotel #latedinner #healthyrecipes
Jesscookinitup

Jesscookinitup

302 likes

As we delve deeper into the truth about animal
agriculture, we experience a sense of awakening, a newfound understanding of the intricate web of life and our place within it. Day In My Life 📅 Date: x 🌅Morning Routine: 🌤Afternoon Routine: 🌃Night Routine: Activities to-do within it. This awakening is characterized by: - Compassion:
Dylan Johnson

Dylan Johnson

15 likes

Why I invested in a camera for my content
Old iphone When I first invested in a camera, all I had was an iphone 7. I was on a cellphone family plan and unemployed during the pandemic, but I wanted to start making content as my hobby.So, when the US government paid unemployed peoples $600/week I took that chance to invest in my content cre
desireealicea

desireealicea

17 likes

10 April Cozy Game Releases
Hi, cozy friends ✨️ April is a big month for cozy games! Here are 10 games releasing throughout this month. Are you excited for any, or have you already gotten your hands on one or two? Let me know! I've played Botany Manor and loved it! #cozygames #cozygamer #nintendoswitch #ga
Cozy Gamer Cris

Cozy Gamer Cris

333 likes

A Strings of Fate Excerpt
An excerpt from my book of poetry, Strings of Fate. Now available on Amazon, Kindle, KU, Kobo, and Barns & Noble 🖤 #bookrecommendations #author #soulmate #poetry #indieauthor
Author Frances

Author Frances

7 likes

That girl podcasts ✨️ you will love ✨️
#lemon8tainment #myplaylist
Emma Taylor

Emma Taylor

24 likes

Quotes I LOVE as a Pre-nursing Student🩻🩺
If you’re a pre-nursing student like me, you may look courage at times. Quotes are known to give some people that extra BOOST to keep them going. The best thing for students is to stay motivated. Here’s how I found quotes and encouragement! BeFreed is the #1 smart reading app that condenses n
Naomilily

Naomilily

0 likes

See more