string = "hello world" print(string[2:5]) # hello in python

To print "hello" from the string "hello world" in python using slicing, we can do the following:

main.py
string = "hello world"
print(string[0:5]) # prints "hello"
59 chars
3 lines

Here, we are using the slicing operator on the string variable to print the characters between index positions 0-4 (inclusive), i.e., "hello".

related categories

gistlibby LogSnag