Kursinnehåll
Introduction to Python (copy)
Introduction to Python (copy)
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.
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)
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.
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)
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 stringgrocery_item
and store it inlength_of_item
. - Use positive indexing to get the first character of each word in
grocery_item
, and assign them tofirst_char
,second_char
, andthird_char
. - Use negative indexing to get the last character of each word, and assign them to
last_char1
,last_char2
, andlast_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
Tack för dina kommentarer!
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.
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)
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.
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)
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 stringgrocery_item
and store it inlength_of_item
. - Use positive indexing to get the first character of each word in
grocery_item
, and assign them tofirst_char
,second_char
, andthird_char
. - Use negative indexing to get the last character of each word, and assign them to
last_char1
,last_char2
, andlast_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
Tack för dina kommentarer!