Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Slice the Word | Python String Manipulation
Data Types in Python
course content

Kursinnhold

Data Types in Python

Data Types in Python

1. Getting Familiar With Numbers in Python
2. Mastering Boolean Logic in Python
3. Python String Manipulation
4. Bring All the Topics Together

book
Slice the Word

Slicing allows you to extract a substring — a sequence of characters from a string. Unlike indexing (which gives one character), slicing returns a portion of the string.

python

start — the index where the slice begins (inclusive)

end — the index where the slice stops (exclusive)

So the character at start is included, but the character at end is not.

123
record = "Invoice INV-2024-007 paid" invoice_code = record[8:21] print(invoice_code) # Output: INV-2024-007
copy

In this case:

  • 'I' is at index 8;
  • '7' is at index 20;

So we use [8:21] to get the full invoice code

Oppgave

Swipe to start coding

Given the string:

"asset and liability, debit and credit"

  1. Use slicing to extract the word "asset" and assign it to the variable asset.

  2. Use slicing to extract the word "debit" and assign it to the variable debit. Make sure to use slicing only — no string methods or manual typing.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 4
toggle bottom row

book
Slice the Word

Slicing allows you to extract a substring — a sequence of characters from a string. Unlike indexing (which gives one character), slicing returns a portion of the string.

python

start — the index where the slice begins (inclusive)

end — the index where the slice stops (exclusive)

So the character at start is included, but the character at end is not.

123
record = "Invoice INV-2024-007 paid" invoice_code = record[8:21] print(invoice_code) # Output: INV-2024-007
copy

In this case:

  • 'I' is at index 8;
  • '7' is at index 20;

So we use [8:21] to get the full invoice code

Oppgave

Swipe to start coding

Given the string:

"asset and liability, debit and credit"

  1. Use slicing to extract the word "asset" and assign it to the variable asset.

  2. Use slicing to extract the word "debit" and assign it to the variable debit. Make sure to use slicing only — no string methods or manual typing.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 4
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt