Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте String Indexing and Length | Section
Python Basics for Data Analysis
Секція 1. Розділ 8
single

single

bookString 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
Завдання

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.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 8
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt