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,love
starts 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 Program to Find LCM
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Multiple Inheritance
Python Program to Find Factorial of Number Using Recursion
Python Program to Multiply Two Matrices
Python Strings
Converting a Stack Trace to a String in Java
JavaScript Methods of RegExp and String
Python hash()
Python next()
Python Program to Create Pyramid Patterns
CharSequence vs. String in Java
Python String strip()
Python String isalpha()
Python Program to Display Fibonacci Sequence Using Recursion
Python divmod()
Python Program to Catch Multiple Exceptions in One Line
Generate a String
Python Dictionary keys()
Python Tuple
Python String split()
Python String lower()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python String rfind()
Python dir()
Python Set intersection()
Python Program to Add Two Numbers
Python Recursion
Python *args and **kwargs
Python Program to Check Armstrong Number
Python format()
Encode a String to UTF-8 in Java