How to get first 100 characters of the string in Python?

PythonServer Side ProgrammingProgramming

The slice ( : ) operator in Python allows you to obtain a part of string. The slice operator has two operands, index of beginning of slice and end of slice.

substr = var[x:y]

In following example, three characters from 7th characters are obtained ( Python sequence uses zero based index)

>>> var="Hello how are you?"
>>> str1=var[6:9]
>>> str1
'how'

In order to obtain first 100 characters,

>>> str1=var[0:100]
raja
Published on 01-Jan-2018 19:29:24
Advertisements