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 format()
Python String zfill()
String Set Queries
Python Program to Convert String to Datetime
Python Program to Get the Full Path of the Current Working Directory
Array to String Conversions
Python Program to Check Armstrong Number
Debug a JavaMail Program
Python Program to Iterate Over Dictionaries Using for Loop
Python List count()
Python Deep Learning Cookbook - Indra den Bakker
Python String maketrans()
Python memoryview()
Python open()
Python String index()
Python break and continue
Python RegEx
Python exec()
Python Objects and Classes
Python *args and **kwargs
Python Program to Count the Number of Each Vowel
Python String startswith()
Generate a String
Python strptime()
Python Dictionary
String Processing with Apache Commons Lang 3
Python callable()
Python Program to Return Multiple Values From a Function
Python List index()
Split a String in Java
Converting a Stack Trace to a String in Java
Converting a List to String in Java