Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære String Indexing in Python | Variables and Types in Python
Introduction to Python(ihor)

bookString Indexing in Python

To access a specific character in a string, use square brackets with an index number inside. Python uses zero-based indexing, meaning the index number does not correspond directly to the character's position.

12345
# The string of digits word = "codefinity" # Access the character at index 1 (second character) print(word[1])
copy

Negative indexing in Python allows you to access elements from the end. Instead of starting from the beginning (index 0), negative indexing starts from the end (index -1), which represents the last element.

12345
# The string of digits word = "codefinity" # Access the characters at index -1 and -9 (first and last ones) print(word[-1], word[-10])
copy
question mark

Given the string what character is at index [4] and [-8]?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Spørg mig spørgsmål om dette emne

Opsummér dette kapitel

Vis virkelige eksempler

Awesome!

Completion rate improved to 1.67

bookString Indexing in Python

Stryg for at vise menuen

To access a specific character in a string, use square brackets with an index number inside. Python uses zero-based indexing, meaning the index number does not correspond directly to the character's position.

12345
# The string of digits word = "codefinity" # Access the character at index 1 (second character) print(word[1])
copy

Negative indexing in Python allows you to access elements from the end. Instead of starting from the beginning (index 0), negative indexing starts from the end (index -1), which represents the last element.

12345
# The string of digits word = "codefinity" # Access the characters at index -1 and -9 (first and last ones) print(word[-1], word[-10])
copy
question mark

Given the string what character is at index [4] and [-8]?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7
some-alt