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 oct()
Python Program to Check Whether a String is Palindrome or Not
Python String find()
Python Program to Copy a File
Python Program to Capitalize the First Character of a String
Python pass statement
Python Set update()
How to get current date and time in Python?
Python str()
Python Program to Create Pyramid Patterns
Python chr()
Python Program to Display the multiplication Table
Python Type Conversion and Type Casting
Python classmethod()
Python Tuple
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Machine Learning - Sebastian Raschka
Python Program to Find LCM
Case-Insensitive String Matching in Java
Java InputStream to String
Python String ljust()
Python open()
Why String is Immutable in Java?
Python String maketrans()
Python String title()
Python Program to Get the File Name From the File Path
Python bin()
Converting a List to String in Java
Python List Comprehension
Python Set copy()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Find ASCII Value of Character