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:
Converting a List to String in Java
Python delattr()
Python time Module
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python break and continue
APIs in Node.js vs Python - A Comparison
Python timestamp to datetime and vice-versa
Python Program to Check the File Size
Python Program to Compute all the Permutation of the String
Python Program to Differentiate Between type() and isinstance()
Python filter()
Python Program to Find Numbers Divisible by Another Number
Python str()
Python Program to Get the Class Name of an Instance
Python Program to Convert Bytes to a String
Python Dictionary update()
Python Set pop()
Python List clear()
String Hashing
Python bool()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Java – String to Reader
Python Program to Find the Largest Among Three Numbers
Check if a String is a Palindrome in Java
Python Program to Randomly Select an Element From the List
Python Directory and Files Management
Python String title()
Python Set intersection()
Python type()
Python Tuple count()
Python Program to Find Armstrong Number in an Interval
Python Decorators