Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте String Slicing and Indexing | Section 2
Python - Music

Свайпніть щоб показати меню

book
String Slicing and Indexing

Indexing is like selecting a specific note from your melody. Each character in a string has a position, starting from 0. So, if you want to find the first character in song_title, you use:

123
song_title = "Bohemian Rhapsody" first_letter = song_title[0] print("First letter:", first_letter)
copy

Note

Python uses zero-based indexing, so the first character is at position 0, the second at position 1, and so on.

Slicing is like creating a melody by selecting a range of notes. You can extract a substring by specifying a start and end index. For example, if you want to extract "Bohemian" from song_title, you can slice it like this:

123
song_title = "Bohemian Rhapsody" melody = song_title[0:8] print("Melody:", melody)
copy

The slice 0:8 means "start at index 0 and go up to, but not including, index 8."

Just like a DJ can remix a track, you can use slicing to create new variations. You can even specify a step to skip characters, like skipping every other note:

123
song_title = "Bohemian Rhapsody" remix = song_title[0:8:2] print("Remix:", remix)
copy

Sometimes, you want to rewind and start from the end of the track. Negative Indexing allows you to do just that. For example, to get the last character:

123
song_title = "Bohemian Rhapsody" last_letter = song_title[-1] print("Last letter:", last_letter)
copy
Завдання

Swipe to start coding

Complete the extract_first_characters function that extracts and returns the first characters_count characters from a given track name. This is useful for creating previews or summaries of track names.

Inputs:

  • track_name: A string representing the name of the track.
  • characters_count: An integer specifying the number of characters to extract from the beginning of the track name.

Steps:

  • Extract Characters: Use string slicing to extract the first characters_count characters from track_name.

Рішення

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

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

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

Секція 2. Розділ 1
py

solution.py

py

main.py

Ми дуже хвилюємося, що щось пішло не так. Що трапилося?

Запитати АІ

expand
ChatGPT

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

book
String Slicing and Indexing

Indexing is like selecting a specific note from your melody. Each character in a string has a position, starting from 0. So, if you want to find the first character in song_title, you use:

123
song_title = "Bohemian Rhapsody" first_letter = song_title[0] print("First letter:", first_letter)
copy

Note

Python uses zero-based indexing, so the first character is at position 0, the second at position 1, and so on.

Slicing is like creating a melody by selecting a range of notes. You can extract a substring by specifying a start and end index. For example, if you want to extract "Bohemian" from song_title, you can slice it like this:

123
song_title = "Bohemian Rhapsody" melody = song_title[0:8] print("Melody:", melody)
copy

The slice 0:8 means "start at index 0 and go up to, but not including, index 8."

Just like a DJ can remix a track, you can use slicing to create new variations. You can even specify a step to skip characters, like skipping every other note:

123
song_title = "Bohemian Rhapsody" remix = song_title[0:8:2] print("Remix:", remix)
copy

Sometimes, you want to rewind and start from the end of the track. Negative Indexing allows you to do just that. For example, to get the last character:

123
song_title = "Bohemian Rhapsody" last_letter = song_title[-1] print("Last letter:", last_letter)
copy
Завдання

Swipe to start coding

Complete the extract_first_characters function that extracts and returns the first characters_count characters from a given track name. This is useful for creating previews or summaries of track names.

Inputs:

  • track_name: A string representing the name of the track.
  • characters_count: An integer specifying the number of characters to extract from the beginning of the track name.

Steps:

  • Extract Characters: Use string slicing to extract the first characters_count characters from track_name.

Рішення

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

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

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

Секція 2. Розділ 1
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Ми дуже хвилюємося, що щось пішло не так. Що трапилося?
some-alt