Course Content
Python Data Types
Python Data Types
String Slicing 2/2
We can also get a substring from a string using simple slicing.
First, let's look at the syntax of slicing.
string[start: end: step]
respectively:
- start -- the index from which to start slicing.
- end -- the index at which the slicing ends.
- step -- this parameter determines the increments between the indices.
Ending index is
up to but not including
.
Indexing starts from 0.
It's time for an example.
# Creating a string string = 'ABCDEFGHIJKLMNO' print("Initial string: ", string) string_1 = string[7:13] print("Elements in a range 7-13: ", string_1)
# Print elements from a pre-defined point to end string_2 = string[5:] print("Elements from 6th element till the end: ", string_2)
# Print all elements from beginning till end string_3 = string[:] print("All elements: ", string_3)
Swipe to show code editor
You have such a string 1234567890
.
You have to carry out the following steps:
- print elements from beginning till the end(as a result you have to get:
1234567890
) - print elements from the 5th element till the end(as a result you have to get:
567890
) - print elements in a range 2-7(as a result you have to get:
34567
)
Thanks for your feedback!
String Slicing 2/2
We can also get a substring from a string using simple slicing.
First, let's look at the syntax of slicing.
string[start: end: step]
respectively:
- start -- the index from which to start slicing.
- end -- the index at which the slicing ends.
- step -- this parameter determines the increments between the indices.
Ending index is
up to but not including
.
Indexing starts from 0.
It's time for an example.
# Creating a string string = 'ABCDEFGHIJKLMNO' print("Initial string: ", string) string_1 = string[7:13] print("Elements in a range 7-13: ", string_1)
# Print elements from a pre-defined point to end string_2 = string[5:] print("Elements from 6th element till the end: ", string_2)
# Print all elements from beginning till end string_3 = string[:] print("All elements: ", string_3)
Swipe to show code editor
You have such a string 1234567890
.
You have to carry out the following steps:
- print elements from beginning till the end(as a result you have to get:
1234567890
) - print elements from the 5th element till the end(as a result you have to get:
567890
) - print elements in a range 2-7(as a result you have to get:
34567
)
Thanks for your feedback!
String Slicing 2/2
We can also get a substring from a string using simple slicing.
First, let's look at the syntax of slicing.
string[start: end: step]
respectively:
- start -- the index from which to start slicing.
- end -- the index at which the slicing ends.
- step -- this parameter determines the increments between the indices.
Ending index is
up to but not including
.
Indexing starts from 0.
It's time for an example.
# Creating a string string = 'ABCDEFGHIJKLMNO' print("Initial string: ", string) string_1 = string[7:13] print("Elements in a range 7-13: ", string_1)
# Print elements from a pre-defined point to end string_2 = string[5:] print("Elements from 6th element till the end: ", string_2)
# Print all elements from beginning till end string_3 = string[:] print("All elements: ", string_3)
Swipe to show code editor
You have such a string 1234567890
.
You have to carry out the following steps:
- print elements from beginning till the end(as a result you have to get:
1234567890
) - print elements from the 5th element till the end(as a result you have to get:
567890
) - print elements in a range 2-7(as a result you have to get:
34567
)
Thanks for your feedback!
We can also get a substring from a string using simple slicing.
First, let's look at the syntax of slicing.
string[start: end: step]
respectively:
- start -- the index from which to start slicing.
- end -- the index at which the slicing ends.
- step -- this parameter determines the increments between the indices.
Ending index is
up to but not including
.
Indexing starts from 0.
It's time for an example.
# Creating a string string = 'ABCDEFGHIJKLMNO' print("Initial string: ", string) string_1 = string[7:13] print("Elements in a range 7-13: ", string_1)
# Print elements from a pre-defined point to end string_2 = string[5:] print("Elements from 6th element till the end: ", string_2)
# Print all elements from beginning till end string_3 = string[:] print("All elements: ", string_3)
Swipe to show code editor
You have such a string 1234567890
.
You have to carry out the following steps:
- print elements from beginning till the end(as a result you have to get:
1234567890
) - print elements from the 5th element till the end(as a result you have to get:
567890
) - print elements in a range 2-7(as a result you have to get:
34567
)