Posted at: https://laptrinhcanban.com/en
Instructions on how to slice the string in python. You will learn about slice a string python by index, jumping when slicing strings as well as reduce arguments when slicing string in python after this lesson.
Slice a string in python by index
To slice a string in python by index, we will specify the starting position to cut the string and the end position from the original string to get the substring, according to the following syntax:
str
[ start position
: end position
]
Inside
- str is the original string from which we want to slice
start position
is the index of the character from which we will begin cutting the string.end position
is the index of the character we will end up trimming at that position plus 1 unit
🌟 Note that the cut out string does not include any characters at end position, so when specifying, we need to add 1 unit to end position. Thus, the cut string will include the characters in end position.
🌟 About the index numbering of the character in the string, start the index numbering of the leftmost character with 0, then add 1 and move forward to the right. Note that the index numbering starts from 0, not from 1.
You can see more about specifying an index in python string at the pos Get character in python string.
Slice the string in python example
Suppose we need to cut the string city
and create a new string from the original string bigcityboy
.
The index numbering of the characters in the original string.
The index value of the characters in the string isbigcityboy
as follows:b i g c i t y b o y ------------------- 0 1 2 3 4 5 6 7 8 9
Determination
start position
.
In the above example, we start cutting at the position of the characterc
(the starting character of the string city) whose index is 3.
Hencestart position
by this index value.Determination
end position
.
In the above example, we will cut to the position of the charactery
whose index is 6.
In order for the newly created string to contain this character, we need to add 1 unit so the value ofend position
will be 7.Write a cut order.
In this example would bebigcityboy[3:7]
We write the sample code as follows:
str = "bigcityboy" |
Result
city
Similarly, we can cut other strings and create new string from the original string as follows:
str = "bigcityboy" |
Reduce arguments when slicing string in python
In some cases we can omit the argument start position
or end position
as itshown below.
Reduce start position
when slicing string in python
We can omit start position
when cutting string in python in case of start position
equal 0, which means we will cut from the beginning of the original string.
For example:
Ví dụ:
str = "bigcityboy" |
We can also specify the start position
as following:
str = "bigcityboy" |
Both spellings produce the same result:
bigcity |
Reduce end position
when slicing string in python
We can omit end position
when cutting the string in python in case end position
equal to the length of the original string, which means we will cut until the end of the original string.
For example:
str = "bigcityboy" |
Chúng ta cũng có thể chỉ định vị trí kết thúc
như sau:
str = "bigcityboy" |
Both spellings produce the same result:
cityboy |
- About how to calculate the length of the string, you can refer to the article on calculating the length of the string in python.
Reduce both start position
and end position
when slicing string in python
When we dropped both start position
and end position
when slicing string in python, a new string will create exactly the original string.
For example:
str = "bigcityboy" |
We can also specify start position
and end position
as follows:
str = "bigcityboy" |
Both spellings produce the same result:
bigcityboy |
Jumping when slicing strings in python
When cutting string in python, by default we will get all characters from start position to end position when cutting.
However, we can also jump when cutting strings in python.
For example we can take one character and skip the next 2 characters, then get the character again and remove the next 2 characters and so on.
Once there, we need to specify an additional parameter step
when cutting the string in python, with the following syntax:
string
[ start position
: end position
: step
]
For example, together with the string bigcityboy
above , we can jump 2 steps when cutting the string like this:
str = "bigcityboy" |
Result
ict cyy
Summary
We can cut the string in python by specifying the start and end position of the cut in the original string.
Also when cutting the string, we can jump by specifying an extra number of hops when writing the string trimming command in python.
URL Link
HOME › python for beginners>>string in python