How can we get substring from a string in Python?

PythonServer Side ProgrammingProgramming

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]

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)

Published on 11-Jan-2018 23:51:03