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:
Converting a List to String in Java
Python String lower()
Python open()
Python Program to Compute the Power of a Number
Python Program to Display Powers of 2 Using Anonymous Function
Python strftime()
Python locals()
Python divmod()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python String isspace()
String Processing with Apache Commons Lang 3
Python Anonymous / Lambda Function
Split a String in Java
Python *args and **kwargs
Python Program to Find All File with .txt Extension Present Inside a Directory
Python String isdecimal()
Python String lstrip()
Python Set symmetric_difference()
Python Program to Count the Number of Occurrence of a Character in String
Reading an HTTP Response Body as a String in Java
Python Set copy()
Python filter()
Python Directory and Files Management
Deep Learning with Python - Francois Chollet
Python Set difference_update()
Python Program to Concatenate Two Lists
Python Set clear()
Python Program to Find the Square Root
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Create Pyramid Patterns
Python repr()
Python String index()