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

Swipe to show menu

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

Task

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.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

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

Task

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.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt