In this example, you will learn to get a substring of a string.
To understand this example, you should have the knowledge of the following Python programming topics:
Using String slicing
my_string = "I love python." # prints "love" print(my_string[2:6]) # prints "love python." print(my_string[2:]) # prints "I love python" print(my_string[:-1])
Output
love love python. I love python
String slicing works similar to list slicing. The working of above code can be understood in the following points.
[2:6]
You need to specify the starting index and the ending index of the substring. In this case,lovestarts at index 2 and ends at index 6.[2:]
All the text from index 2 to the end are selected.[:-1]
All the text before the last index is selected.
Related posts:
Python String isdecimal()
Python @property decorator
How to Get Started With Python?
Python Program Read a File Line by Line Into a List
Python Set pop()
Python print()
Python Set union()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python String isalpha()
JavaScript Eval: run a code string
Python Multiple Inheritance
Python Program to Get the Full Path of the Current Working Directory
Python Program to Display the multiplication Table
Python List extend()
Python Operator Overloading
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Count the Number of Digits Present In a Number
Python Dictionary values()
Python Program to Sort Words in Alphabetic Order
Python Program to Shuffle Deck of Cards
Python String expandtabs()
Python Program to Check Leap Year
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python ascii()
Convert String to int or Integer in Java
Generate a String
Python Program to Find the Largest Among Three Numbers
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python strftime()