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

To print the substring "hello" from the given string "hello world" using Python, we can use string slicing.

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

In Python, we can access individual characters of a string using their position, which is known as string indexing. The indexing starts from 0, and we can specify the starting and ending position of the substring to be extracted using slicing. In the above code snippet, string[0:5] refers to the substring of the given string from index 0 to index 4, which contains the characters "hello".

related categories

gistlibby LogSnag