In Python, where does indexing of characters in a string start?

0160

In Python, indexing of characters in a string starts at 0. This means that the first character of the string has an index of 0, the second character has an index of 1, and so on.

Example:

text = "Hello"
print(text[0])  # Output: H (first character)
print(text[1])  # Output: e (second character)
print(text[4])  # Output: o (fifth character)

Additionally, Python supports negative indexing, where -1 refers to the last character, -2 refers to the second last character, and so forth.

Example of Negative Indexing:

print(text[-1])  # Output: o (last character)
print(text[-2])  # Output: l (second last character)

This feature allows for flexible access to string characters from both ends. If you have more questions about string manipulation or indexing, feel free to ask!

0 Comments

no data
Be the first to share your comment!