Substring (a part of text)

To use part of a string, as a developer, I need to use [fromChar:toChar]


The middle

A substring can cut a part out of a string:

text1 = "hello world"
print( text1[1:10] )

will print:

ello worl

It happens because text[1,10] the cut starts after the 1st letter (w)

and stops after 10th letter (l) - which is before 11th letter (d)

the first part:

if we want the first word then we can write: (we don't need to write the 0)

text1 = "hello world"
print( text1[:5] )

will give the result:


The last part

if we want the last word then we can write:

text1 = "hello world"
print( text1[6:11] )

or use negative numbers (they count numbers from the other side):

text1 = "hello world"
print( text1[-5:] )

and will give the result:




Python code

Terminal output

Written by: Bartek / Bartlomiej Rohard Warszawski