1.

What is slicing?

Answer»

String slicing:

Slice is a substring of a main string. A substring can be taken from the original string by using [ ] operator and index or subscript values. Thus, [ ] is also known as slicing operator. Using slice operator, we can slice one or more substrings from a main string.

General format of slice operation:

str[start:end]

Where start is the beginning index and end is the last index value of a character in the string. Python takes the end value less than one from the actual index specified. For example, if you want to slice first 4 characters from a string, you have to specify it as 0 to 5. Because, python consider only the end value as n –1.

Example:

slice a single character from a string

>>> str1= ”THIRUKKURAL”

>>> print (str1[0])

T



Discussion

No Comment Found