Computer >> Computer tutorials >  >> Programming >> Python

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


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]