- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How can we get substring from a string in Python?
In Python, string data type is an ordered sequence of characters. All sequence data types support slicing. Using the slice operator “:”, a substring can be obtained from main string.
>>> string="Hello How are you?" >>> string[6:9] 'How'
The slice operator − requires two operands. First operand is the index of first character of desired substring. The resultant substring contains main string’s characters at indices upto but not including second operand. In above example, characters at index 6 to 8 will form the substring. (Index count starts from 0)
- Related Articles
- How can we extract a substring from a string in MySQL?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- How to extract a substring from inside a string in Python?
- Golang Program to get a substring from the string
- In MySQL, how can we insert a substring at the specified position in a string?
- How to get substring of a string in jQuery?
- Can Make Palindrome from Substring in Python
- How can we extract a substring from the value of a column in MySQL table?
- How to check if string or a substring of string starts with substring in Python?
- How do I remove a substring from the end of a string in Python?
- Program to find how max score we can get by removing 10 or 01 from binary string in Python
- How to find a substring from a string in C#?
- How we can break a string with multiple delimiters in Python?
- How to get integer values from a string in Python?