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:
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Multiply Two Matrices
Python Program to Find the Largest Among Three Numbers
Python String isspace()
Python List count()
Python Data Types
Python Set add()
Python Program to Find LCM
Python String ljust()
Python any()
Python Program to Represent enum
Python Program to Measure the Elapsed Time in Python
Python Dictionary setdefault()
Python String format_map()
Python List insert()
Python Program to Find ASCII Value of Character
Python Program to Delete an Element From a Dictionary
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Adding a Newline Character to a String in Java
Python Get Current time
Python @property decorator
Python locals()
Python Program to Check the File Size
Debug a JavaMail Program
Python String find()
Python Program to Check Armstrong Number
Python format()
Java Program to Implement the Program Used in grep/egrep/fgrep
Python exec()
Python Dictionary keys()
Python String center()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel