Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Viipalointitehtävä | Merkkijonot
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Tietotyypit Pythonissa

bookViipalointitehtävä

Slicing can be enhanced with a step argument, which allows you to extract characters at specific intervals.

This is useful when you want to select every second character, skip letters, or extract patterns from structured data like invoice numbers, transaction codes, or reports.

string[start : end : step]
  • start: the index where slicing begins (inclusive)
  • end: the index where slicing ends (exclusive)
  • step: the interval (e.g., 2 means every second character)

You can also omit one or more values:

  • string[::2] takes every second character from start to end;
  • string[5::3] starts at index 5 and takes every third character;
  • string[:-2:2] takes every second character from the beginning up to (but not including) the second-to-last character.

Example

123
code = "TXN-2024-INV-0007" sliced = code[0:13:5] print(sliced) # Output: T0N
copy
  • We start at index 0
  • End at index 13 (exclusive)
  • Use a step of 5, which means: take every fifth character

Characters at indexes 0, 5, and 10 are 'T', '0', and 'N', so the output is 'T0N'.

This kind of slicing could be used to extract initials or control codes from structured strings in financial or document systems.

Tehtävä

Swipe to start coding

In an accounting system, filenames for financial reports are generated automatically. Each filename includes a prefix, the report type, and the year.

Your task is to use slicing with a step to extract the 1st and 3rd digits of the year (in this case, 2 and 2) from the filename. This result will serve as a simple year control code.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 7
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain how negative steps work in slicing?

What happens if the step value is larger than the string length?

Can you show more examples of using slicing with different step values?

close

bookViipalointitehtävä

Pyyhkäise näyttääksesi valikon

Slicing can be enhanced with a step argument, which allows you to extract characters at specific intervals.

This is useful when you want to select every second character, skip letters, or extract patterns from structured data like invoice numbers, transaction codes, or reports.

string[start : end : step]
  • start: the index where slicing begins (inclusive)
  • end: the index where slicing ends (exclusive)
  • step: the interval (e.g., 2 means every second character)

You can also omit one or more values:

  • string[::2] takes every second character from start to end;
  • string[5::3] starts at index 5 and takes every third character;
  • string[:-2:2] takes every second character from the beginning up to (but not including) the second-to-last character.

Example

123
code = "TXN-2024-INV-0007" sliced = code[0:13:5] print(sliced) # Output: T0N
copy
  • We start at index 0
  • End at index 13 (exclusive)
  • Use a step of 5, which means: take every fifth character

Characters at indexes 0, 5, and 10 are 'T', '0', and 'N', so the output is 'T0N'.

This kind of slicing could be used to extract initials or control codes from structured strings in financial or document systems.

Tehtävä

Swipe to start coding

In an accounting system, filenames for financial reports are generated automatically. Each filename includes a prefix, the report type, and the year.

Your task is to use slicing with a step to extract the 1st and 3rd digits of the year (in this case, 2 and 2) from the filename. This result will serve as a simple year control code.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 7
single

single

some-alt