Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
String Indexing and Length | Variables and Types
Introduction to Python Video Course
course content

Course Content

Introduction to Python Video Course

Introduction to Python Video Course

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

String Indexing and Length

Strings in Python are sequences of characters where each character, including spaces, is assigned a specific position or index. Learning to access these characters using indexing and determining the length of strings using the len() function are fundamental skills in Python.

Watch the following video, where Alex demonstrates how indexing and the len() function can be used to interact with strings effectively.

In Python, indexing starts at 0, so the first character of a string is at index 0, the second at index 1, and so on. This is often referred to as the n-1 rule, where n is the number of characters in the string. To better visualize this, consider the string "APPLE":

A (at index 0) P (at index 1) P (at index 2) L (at index 3) E (at index 4)

Negative Indexing

Conversely, negative indexing allows you to count characters from the end of the string instead of the beginning.

This method is particularly useful when you want to access the last elements of a string without knowing its exact length. The last character of the string is indexed as -1, the second to last as -2, and so forth.

Let's explore the same string, Apple, using negative indexes to highlight how each character can be accessed from the end:

A (at index -5) P (at index -4) P (at index -3) L (at index -2) E (at index -1)

Example Application

Let's start with the basics of string indexing. Use this example to try printing different characters from the string. You can try using negative indexing as well.

12345678
grocery_item = "Milk" # Accessing the first and last character using indexing first_character = grocery_item[0] # 'M' last_character = grocery_item[-1] # 'k', using negative indexing for the last character print("First character:", first_character) print("Last character:", last_character)
copy

Now let's explore a string with spaces and use the len() function to see how spaces are counted as characters.

Understanding that spaces are considered characters in Python can help accurately manipulate strings, especially when they form part of the data.

12345678910
store_name = "Green Valley Market" # Find the length of the string, which includes spaces length_of_name = len(store_name) # Includes spaces in the count # Accessing a character in a position after a space character_after_space = store_name[6] # 'V' print("Length of store name:", length_of_name) print("Character after the space:", character_after_space)
copy

Task

Now that you understand the basics of string indexing and the importance of considering spaces, try this more complex challenge. Using the defined string, determine the string length using the len() function and print the first character and the last character of each word using indexing.

Task

Now that you understand the basics of string indexing and the importance of considering spaces, try this more complex challenge. Using the defined string, determine the string length using the len() function and print the first character and the last character of each word using indexing.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 2. Chapter 5
toggle bottom row

String Indexing and Length

Strings in Python are sequences of characters where each character, including spaces, is assigned a specific position or index. Learning to access these characters using indexing and determining the length of strings using the len() function are fundamental skills in Python.

Watch the following video, where Alex demonstrates how indexing and the len() function can be used to interact with strings effectively.

In Python, indexing starts at 0, so the first character of a string is at index 0, the second at index 1, and so on. This is often referred to as the n-1 rule, where n is the number of characters in the string. To better visualize this, consider the string "APPLE":

A (at index 0) P (at index 1) P (at index 2) L (at index 3) E (at index 4)

Negative Indexing

Conversely, negative indexing allows you to count characters from the end of the string instead of the beginning.

This method is particularly useful when you want to access the last elements of a string without knowing its exact length. The last character of the string is indexed as -1, the second to last as -2, and so forth.

Let's explore the same string, Apple, using negative indexes to highlight how each character can be accessed from the end:

A (at index -5) P (at index -4) P (at index -3) L (at index -2) E (at index -1)

Example Application

Let's start with the basics of string indexing. Use this example to try printing different characters from the string. You can try using negative indexing as well.

12345678
grocery_item = "Milk" # Accessing the first and last character using indexing first_character = grocery_item[0] # 'M' last_character = grocery_item[-1] # 'k', using negative indexing for the last character print("First character:", first_character) print("Last character:", last_character)
copy

Now let's explore a string with spaces and use the len() function to see how spaces are counted as characters.

Understanding that spaces are considered characters in Python can help accurately manipulate strings, especially when they form part of the data.

12345678910
store_name = "Green Valley Market" # Find the length of the string, which includes spaces length_of_name = len(store_name) # Includes spaces in the count # Accessing a character in a position after a space character_after_space = store_name[6] # 'V' print("Length of store name:", length_of_name) print("Character after the space:", character_after_space)
copy

Task

Now that you understand the basics of string indexing and the importance of considering spaces, try this more complex challenge. Using the defined string, determine the string length using the len() function and print the first character and the last character of each word using indexing.

Task

Now that you understand the basics of string indexing and the importance of considering spaces, try this more complex challenge. Using the defined string, determine the string length using the len() function and print the first character and the last character of each word using indexing.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 2. Chapter 5
toggle bottom row

String Indexing and Length

Strings in Python are sequences of characters where each character, including spaces, is assigned a specific position or index. Learning to access these characters using indexing and determining the length of strings using the len() function are fundamental skills in Python.

Watch the following video, where Alex demonstrates how indexing and the len() function can be used to interact with strings effectively.

In Python, indexing starts at 0, so the first character of a string is at index 0, the second at index 1, and so on. This is often referred to as the n-1 rule, where n is the number of characters in the string. To better visualize this, consider the string "APPLE":

A (at index 0) P (at index 1) P (at index 2) L (at index 3) E (at index 4)

Negative Indexing

Conversely, negative indexing allows you to count characters from the end of the string instead of the beginning.

This method is particularly useful when you want to access the last elements of a string without knowing its exact length. The last character of the string is indexed as -1, the second to last as -2, and so forth.

Let's explore the same string, Apple, using negative indexes to highlight how each character can be accessed from the end:

A (at index -5) P (at index -4) P (at index -3) L (at index -2) E (at index -1)

Example Application

Let's start with the basics of string indexing. Use this example to try printing different characters from the string. You can try using negative indexing as well.

12345678
grocery_item = "Milk" # Accessing the first and last character using indexing first_character = grocery_item[0] # 'M' last_character = grocery_item[-1] # 'k', using negative indexing for the last character print("First character:", first_character) print("Last character:", last_character)
copy

Now let's explore a string with spaces and use the len() function to see how spaces are counted as characters.

Understanding that spaces are considered characters in Python can help accurately manipulate strings, especially when they form part of the data.

12345678910
store_name = "Green Valley Market" # Find the length of the string, which includes spaces length_of_name = len(store_name) # Includes spaces in the count # Accessing a character in a position after a space character_after_space = store_name[6] # 'V' print("Length of store name:", length_of_name) print("Character after the space:", character_after_space)
copy

Task

Now that you understand the basics of string indexing and the importance of considering spaces, try this more complex challenge. Using the defined string, determine the string length using the len() function and print the first character and the last character of each word using indexing.

Task

Now that you understand the basics of string indexing and the importance of considering spaces, try this more complex challenge. Using the defined string, determine the string length using the len() function and print the first character and the last character of each word using indexing.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Strings in Python are sequences of characters where each character, including spaces, is assigned a specific position or index. Learning to access these characters using indexing and determining the length of strings using the len() function are fundamental skills in Python.

Watch the following video, where Alex demonstrates how indexing and the len() function can be used to interact with strings effectively.

In Python, indexing starts at 0, so the first character of a string is at index 0, the second at index 1, and so on. This is often referred to as the n-1 rule, where n is the number of characters in the string. To better visualize this, consider the string "APPLE":

A (at index 0) P (at index 1) P (at index 2) L (at index 3) E (at index 4)

Negative Indexing

Conversely, negative indexing allows you to count characters from the end of the string instead of the beginning.

This method is particularly useful when you want to access the last elements of a string without knowing its exact length. The last character of the string is indexed as -1, the second to last as -2, and so forth.

Let's explore the same string, Apple, using negative indexes to highlight how each character can be accessed from the end:

A (at index -5) P (at index -4) P (at index -3) L (at index -2) E (at index -1)

Example Application

Let's start with the basics of string indexing. Use this example to try printing different characters from the string. You can try using negative indexing as well.

12345678
grocery_item = "Milk" # Accessing the first and last character using indexing first_character = grocery_item[0] # 'M' last_character = grocery_item[-1] # 'k', using negative indexing for the last character print("First character:", first_character) print("Last character:", last_character)
copy

Now let's explore a string with spaces and use the len() function to see how spaces are counted as characters.

Understanding that spaces are considered characters in Python can help accurately manipulate strings, especially when they form part of the data.

12345678910
store_name = "Green Valley Market" # Find the length of the string, which includes spaces length_of_name = len(store_name) # Includes spaces in the count # Accessing a character in a position after a space character_after_space = store_name[6] # 'V' print("Length of store name:", length_of_name) print("Character after the space:", character_after_space)
copy

Task

Now that you understand the basics of string indexing and the importance of considering spaces, try this more complex challenge. Using the defined string, determine the string length using the len() function and print the first character and the last character of each word using indexing.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 5
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt