Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära String Indexing and Length | Variables and Types
Introduction to Python (copy)
course content

Kursinnehåll

Introduction to Python (copy)

Introduction to Python (copy)

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

book
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":

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:

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
Uppgift

Swipe to start coding

Use string indexing to extract specific characters from a given string. Calculate the string's length using len().

  • Use len() to get the length of the string grocery_item and store it in length_of_item.
  • Use positive indexing to get the first character of each word in grocery_item, and assign them to first_char, second_char, and third_char.
  • Use negative indexing to get the last character of each word, and assign them to last_char1, last_char2, and last_char3.

Output Requirements

Print the results in the following formats:

  • Length of item name: <length_of_item>
  • First character of each word: <first_char> <second_char> <third_char>
  • Last character of each word: <last_char1> <last_char2> <last_char3>

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 5
toggle bottom row

book
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":

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:

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
Uppgift

Swipe to start coding

Use string indexing to extract specific characters from a given string. Calculate the string's length using len().

  • Use len() to get the length of the string grocery_item and store it in length_of_item.
  • Use positive indexing to get the first character of each word in grocery_item, and assign them to first_char, second_char, and third_char.
  • Use negative indexing to get the last character of each word, and assign them to last_char1, last_char2, and last_char3.

Output Requirements

Print the results in the following formats:

  • Length of item name: <length_of_item>
  • First character of each word: <first_char> <second_char> <third_char>
  • Last character of each word: <last_char1> <last_char2> <last_char3>

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 5
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt