Slice the string in python | Laptrinhcanban.com

HOME › >>

Slice the string in python

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.

  1. The index numbering of the characters in the original string.
    The index value of the characters in the string is bigcityboy as follows:

     b i g c i t y b o y
     -------------------
     0 1 2 3 4 5 6 7 8 9
    
  2. Determination start position.
    In the above example, we start cutting at the position of the character c(the starting character of the string city) whose index is 3.
    Hence start position by this index value.

  3. Determination end position.
    In the above example, we will cut to the position of the character y whose index is 6.
    In order for the newly created string to contain this character, we need to add 1 unit so the value of end position will be 7.

  4. Write a cut order.
    In this example would be bigcityboy[3:7]

We write the sample code as follows:

str = "bigcityboy"
print (str[3:7])

Result

city

Similarly, we can cut other strings and create new string from the original string as follows:

str = "bigcityboy"
print (str[3:6])
#>> cit

str = "bigcityboy"
print (str[2:4])
#>> gc

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"
print (str[:7])

We can also specify the start position as following:

str = "bigcityboy"
print (str[0:7])

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"
print (str[3:])

Chúng ta cũng có thể chỉ định vị trí kết thúc như sau:

str = "bigcityboy"
print (str[3:10])

Both spellings produce the same result:

cityboy

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"
print (str[:])

We can also specify start position and end position as follows:

str = "bigcityboy"
print (str[0:10])

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"
print (str[3:7:2])

print (str[3:10:3])

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

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/thao-tac-voi-chuoi-string-trong-python/cach-cat-chuoi-trong-python-string-slice-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.