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

In order to obtain first 100 characters,

>>> str1=var[0:100]
Updated on 18-Feb-2020 08:22:26