Python Project Series #50: String - finding index in Python

Опубликовано: 08 Март 2025
на канале: Programming With Reza
5
0

In Python, you can find the position of a substring within a string using the find() and index() methods. Both return the starting index of the first occurrence of the substring, but they handle missing substrings differently.

find() Method
Returns the index if the substring is found; otherwise, it returns -1.

text = "Hello, world!"
print(text.find("world")) # Output: 7
print(text.find("Python")) # Output: -1 (not found)

index() Method
Works similarly to find(), but raises a ValueError if the substring is not found.

print(text.index("world")) # Output: 7
print(text.index("Python")) # Raises ValueError
Both methods accept optional start and end parameters to limit the search range.

print(text.find("o", 5)) # Searches for 'o' starting from index 5

These methods are useful for text processing, data parsing, and substring validation when working with strings in Python.

=====================
🔥 My blue Yeti Microphone For Streaming:
https://amzn.to/3Dt8BrG

🔥 My Coding Keyboard:
https://amzn.to/41pHHZT

🔥 My Vertical Logitech Mouse:
https://amzn.to/43nG1m6

🔥 My 4K Webcam For Streaming:
https://amzn.to/43qwGdf


🔥 Learn C++ and Python from Zero to Hero! 🚀

Are you ready to master C++ or Python from scratch? Whether you're a beginner or looking to sharpen your skills, my comprehensive bootcamps will take you from zero to hero! 💡

🔹 C++ Bootcamp (+30 Hours of teaching and + 200 Coding Exercises): Master C++ fundamentals, object-oriented programming, and advanced techniques. Perfect for competitive programming, system development, and game programming.
👉 Enroll Now with My Special Coupon: https://www.udemy.com/course/learn-c-...

🔹 Python Bootcamp (+24 Hours of teaching and + 200 Coding Exercises): Learn Python for data science, automation, web development, and more. Step-by-step guidance for beginners and professionals.
👉 Enroll Now with My Special Coupon: https://www.udemy.com/course/learn-py...

💡 Why Join?
✅ Beginner-friendly & structured lessons
✅ Hands-on projects and real-world applications
✅ Lifetime access & Q&A support
✅ Learn at your own pace

🎯 Don't miss this opportunity! Start your coding journey today! 🚀


🔹 Learn with Me on Skillshare! 🚀

Expand your coding skills on Skillshare with my in-depth C++ and Python courses! Whether you're a beginner or an experienced programmer, my structured lessons and hands-on exercises will help you master coding efficiently.

👉 Start Learning Today (1 month free trial): https://www.skillshare.com/en/r/user/...
👉 Begin Drafting Your Academic CV for Academic Positions with My Lecture: https://skl.sh/3Xmhz0A


Join now and take your programming journey to the next level! 💡🔥


🚀 Check out my Coding newsletter For FREE on LinkedIn:🎯

  / 7192139690139893760  


Support my work:
If you find this video helpful, consider buying me a coffee: https://buymeacoffee.com/rezaashahih


#Programming #Python#function#CodingTutorial #LeanPython#ProgrammingBasics #CodingForBeginners #SoftwareDevelopment #ComputerScience #cpp #c++



Don't forget to like, comment, and subscribe for more C++ tutorials!