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:
Reading an HTTP Response Body as a String in Java
Python Set intersection_update()
Python isinstance()
Python filter()
Python Program to Merge Mails
Python Dictionary items()
Java – String to Reader
Python Matrices and NumPy Arrays
Python Program to Merge Two Dictionaries
Python String casefold()
Python Tuple count()
Python String isalnum()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Trim Whitespace From a String
Python Dictionary setdefault()
Python Keywords and Identifiers
Python Global Keyword
Python while Loop
Convert String to int or Integer in Java
Python time Module
Python String zfill()
Python Sets
Python Set remove()
Python Program to Access Index of a List Using for Loop
Python Program to Parse a String to a Float or Int
Python Program to Make a Flattened List from Nested List
Deep Learning with Python - Francois Chollet
CharSequence vs. String in Java
Python bin()
Python Errors and Built-in Exceptions
Python Machine Learning Eqution Reference - Sebastian Raschka
Python String isdecimal()