Strings as Arrays

In Python, we can treat text as an array of characters. Each character in the text has its own index, allowing us to access individual characters similarly to array elements.

Let's look at an example:

text = "Python"

In this example, the string text consists of six characters. If we want to access the first character of the string, we use index 0:

first_char = text[0]
print(first_char)  # Output: 'P'

Similarly, we can access other characters:

second_char = text[1]
print(second_char)  # Output: 'y'

last_char = text[-1]
print(last_char)  # Output: 'n'

Index -1 refers to the last character in the array.

Instructions

Store the last character of the variable name in a variable named lastChar.

Print the value of the variable lastChar.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

9/10

Strings as Arrays | Start Coder