Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Strings slicing | Common data types
Learn Python from Scratch
course content

Course Content

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

bookStrings slicing

You can refer to element(s) in your string. To do that, use indexes inside square brackets and note that indexation in Python starts from 0, so for the string with 5 symbols ("Hello"), the last symbol ("o") will have index 4.

Also, you can get multiple symbols in one action. To do so, separate starting and finishing position plus 1 with : (for example, if you want elements with indexes from 1 to 4, you need to write 1:5, as the right limit is exclusive). At the same time, you can refer to elements in reverse order, i.e., call the very last element as the first in reverse order. Use the minus - sign to do it. If you want to know the total number of symbols in your string, use the len() function. Please note that numeration in reverse order starts from 1 (as there is no minus zero, which makes it logical).

For example:

12345678910
# create new string string = "This is string in Python" # let's get the first element print(string[0]) # let's get all the elements from index 3 to 8 print(string[3:9]) # let's get the last but one element print(string[-2]) # let's get the number of symbols in this string print(len(string))
copy

Task

Given string "This is string for learning" assigned to variable test_str. You need to:

  1. Extract the very first and very last symbols.
  2. Extract word string.
  3. Get the length of this string.

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 5
toggle bottom row

bookStrings slicing

You can refer to element(s) in your string. To do that, use indexes inside square brackets and note that indexation in Python starts from 0, so for the string with 5 symbols ("Hello"), the last symbol ("o") will have index 4.

Also, you can get multiple symbols in one action. To do so, separate starting and finishing position plus 1 with : (for example, if you want elements with indexes from 1 to 4, you need to write 1:5, as the right limit is exclusive). At the same time, you can refer to elements in reverse order, i.e., call the very last element as the first in reverse order. Use the minus - sign to do it. If you want to know the total number of symbols in your string, use the len() function. Please note that numeration in reverse order starts from 1 (as there is no minus zero, which makes it logical).

For example:

12345678910
# create new string string = "This is string in Python" # let's get the first element print(string[0]) # let's get all the elements from index 3 to 8 print(string[3:9]) # let's get the last but one element print(string[-2]) # let's get the number of symbols in this string print(len(string))
copy

Task

Given string "This is string for learning" assigned to variable test_str. You need to:

  1. Extract the very first and very last symbols.
  2. Extract word string.
  3. Get the length of this string.

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 5
toggle bottom row

bookStrings slicing

You can refer to element(s) in your string. To do that, use indexes inside square brackets and note that indexation in Python starts from 0, so for the string with 5 symbols ("Hello"), the last symbol ("o") will have index 4.

Also, you can get multiple symbols in one action. To do so, separate starting and finishing position plus 1 with : (for example, if you want elements with indexes from 1 to 4, you need to write 1:5, as the right limit is exclusive). At the same time, you can refer to elements in reverse order, i.e., call the very last element as the first in reverse order. Use the minus - sign to do it. If you want to know the total number of symbols in your string, use the len() function. Please note that numeration in reverse order starts from 1 (as there is no minus zero, which makes it logical).

For example:

12345678910
# create new string string = "This is string in Python" # let's get the first element print(string[0]) # let's get all the elements from index 3 to 8 print(string[3:9]) # let's get the last but one element print(string[-2]) # let's get the number of symbols in this string print(len(string))
copy

Task

Given string "This is string for learning" assigned to variable test_str. You need to:

  1. Extract the very first and very last symbols.
  2. Extract word string.
  3. Get the length of this string.

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!

You can refer to element(s) in your string. To do that, use indexes inside square brackets and note that indexation in Python starts from 0, so for the string with 5 symbols ("Hello"), the last symbol ("o") will have index 4.

Also, you can get multiple symbols in one action. To do so, separate starting and finishing position plus 1 with : (for example, if you want elements with indexes from 1 to 4, you need to write 1:5, as the right limit is exclusive). At the same time, you can refer to elements in reverse order, i.e., call the very last element as the first in reverse order. Use the minus - sign to do it. If you want to know the total number of symbols in your string, use the len() function. Please note that numeration in reverse order starts from 1 (as there is no minus zero, which makes it logical).

For example:

12345678910
# create new string string = "This is string in Python" # let's get the first element print(string[0]) # let's get all the elements from index 3 to 8 print(string[3:9]) # let's get the last but one element print(string[-2]) # let's get the number of symbols in this string print(len(string))
copy

Task

Given string "This is string for learning" assigned to variable test_str. You need to:

  1. Extract the very first and very last symbols.
  2. Extract word string.
  3. Get the length of this string.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt