Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về Phương thức Dải chuỗi Python [] với sự trợ giúp của các ví dụ.
Phương thức
string.strip[[chars]]0 trả về một bản sao của chuỗi bằng cách xóa cả các ký tự dẫn đầu và dấu [dựa trên đối số chuỗi được truyền].
Thí dụ
message = ' Learn Python '
# remove leading and trailing whitespaces
print['Message:', message.strip[]]
# Output: Message: Learn Python
Cú pháp của chuỗi chuỗi []
Cú pháp của phương pháp
string.strip[[chars]]0 là:
string.strip[[chars]]
Dải [] tham số
- Chars [Tùy chọn] - Một chuỗi chỉ định tập hợp các ký tự được xóa khỏi phần bên trái và bên phải của chuỗi.
Phương thức
string.strip[[chars]]0 loại bỏ các ký tự khỏi cả trái và phải dựa trên đối số [một chuỗi chỉ định tập hợp các ký tự sẽ bị xóa].
Lưu ý: Nếu đối số ký tự không được cung cấp, tất cả các không gian trắng dẫn đầu và dấu vết sẽ bị xóa khỏi chuỗi.: If the chars argument is not provided, all leading and trailing whitespaces are removed from the string.
dải [] giá trị trả về
string.strip[[chars]]0 trả về một bản sao của chuỗi với cả hai ký tự dẫn và dấu vết bị tước.
Làm việc của phương thức dải []
- Khi ký tự của chuỗi trong sự không phù hợp bên trái với tất cả các ký tự trong đối số
string.strip[[chars]]
4, nó sẽ ngừng xóa các ký tự hàng đầu. - Tương tự, khi ký tự của chuỗi trong sự không phù hợp với tất cả các ký tự trong đối số
string.strip[[chars]]
4, nó sẽ ngừng xóa các ký tự dấu vết.
Ví dụ: Làm việc của phương thức Dải []
string = ' xoxo love xoxo '
# Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left
# and right of string are removed
print[string.strip[' xoe']]
# Argument doesn't contain space
# No characters are removed.
print[string.strip['stx']]
string = 'android is awesome'
print[string.strip['an']]
Đầu ra
xoxo love xoxo lov xoxo love xoxo droid is awesome
Ở đây, chúng ta có thể thấy rằng biểu thức đầu tiên
string.strip[[chars]]6 mà không có bất kỳ đối số nào đã loại bỏ các không gian trắng khỏi bên trái và bên phải của
string.strip[[chars]]7.
string.strip[[chars]]
8 - Loại bỏ tất cả khoảng trắng,string.strip[[chars]]
9,
0 vàstring = ' xoxo love xoxo ' # Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left # and right of string are removedprint[string.strip[' xoe']]
# Argument doesn't contain space # No characters are removed. print[string.strip['stx']] string = 'android is awesome'print[string.strip['an']]
1 dẫn hoặc kéo theo chuỗi.string = ' xoxo love xoxo ' # Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left # and right of string are removedprint[string.strip[' xoe']]
# Argument doesn't contain space # No characters are removed. print[string.strip['stx']] string = 'android is awesome'print[string.strip['an']]
2 - Vìstring = ' xoxo love xoxo ' # Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left # and right of string are removedprint[string.strip[' xoe']]
# Argument doesn't contain space # No characters are removed. print[string.strip['stx']] string = 'android is awesome'print[string.strip['an']]
string.strip[[chars]]
7 có khoảng trắng ở đầu và cuối, biểu thức này không thay đổi chuỗi.string.strip[[chars]]
9 không bị loại bỏ vì nó ở giữa chuỗi [Dây dẫn trắng và Trail the String]
5 - Xóastring = ' xoxo love xoxo ' # Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left # and right of string are removedprint[string.strip[' xoe']]
# Argument doesn't contain space # No characters are removed. print[string.strip['stx']] string = 'android is awesome'print[string.strip['an']]
6 dẫn đầu chuỗi.string = ' xoxo love xoxo ' # Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left # and right of string are removedprint[string.strip[' xoe']]
print[string.strip['an']]
Phương thức chuỗi
Thí dụ
Tháo các khoảng trống ở đầu và ở cuối chuỗi:
txt = "& nbsp; & nbsp; & nbsp; & nbsp; chuối & nbsp; & nbsp; & nbsp; & nbsp;"
x = txt.strip []
in ["của tất cả các loại trái cây", x, "là yêu thích của tôi"]
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương pháp
string.strip[[chars]]0 loại bỏ bất kỳ ký tự nào [không gian ở đầu] và các ký tự [không gian ở cuối] [không gian là ký tự dẫn mặc định để loại bỏ]
Cú pháp
Giá trị tham số
nhân vật | Không bắt buộc. Một tập hợp các ký tự để xóa dưới dạng các ký tự dẫn đầu/theo dõi |
Nhiều ví dụ hơn
Thí dụ
Tháo các khoảng trống ở đầu và ở cuối chuỗi:
txt = "& nbsp; & nbsp; & nbsp; & nbsp; chuối & nbsp; & nbsp; & nbsp; & nbsp;"
x = txt.strip []
in ["của tất cả các loại trái cây", x, "là yêu thích của tôi"]
Hãy tự mình thử »
Phương thức chuỗi
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc is an inbuilt function in Python programming language that returns a copy of the string with both leading and trailing
characters removed [based on the string argument passed].
Syntax:
string.strip[[chars]] Parameter: There is only one optional parameter in it: 1]chars - a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string. Return Value: Returns a copy of the string with both leading and trailing characters removed.
Python3
string = ' xoxo love xoxo '
# Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left
# and right of string are removed
print[string.strip[' xoe']]
# Argument doesn't contain space
# No characters are removed.
print[string.strip['stx']]
string = 'android is awesome'
print[string.strip['an']]
8string = ' xoxo love xoxo '
# Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left
# and right of string are removed
print[string.strip[' xoe']]
# Argument doesn't contain space
# No characters are removed.
print[string.strip['stx']]
string = 'android is awesome'
print[string.strip['an']]
9xoxo love xoxo lov xoxo love xoxo droid is awesome0
xoxo love xoxo lov xoxo love xoxo droid is awesome1
xoxo love xoxo lov xoxo love xoxo droid is awesome0
xoxo love xoxo lov xoxo love xoxo droid is awesome3
xoxo love xoxo lov xoxo love xoxo droid is awesome0
xoxo love xoxo lov xoxo love xoxo droid is awesome5
xoxo love xoxo lov xoxo love xoxo droid is awesome6
xoxo love xoxo lov xoxo love xoxo droid is awesome7
Output:
geeks for geeks geeks for geeks for
Python3
Bàn luận
xoxo love xoxo lov xoxo love xoxo droid is awesome0
string.strip[[chars]] Parameter: There is only one optional parameter in it: 1]chars - a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string. Return Value: Returns a copy of the string with both leading and trailing characters removed.2
Dải [] là một hàm sẵn có trong ngôn ngữ lập trình Python trả về một bản sao của chuỗi với cả các ký tự dẫn và dấu vết được xóa [dựa trên đối số chuỗi được truyền]. & nbsp; cú pháp: & nbsp;
xoxo love xoxo lov xoxo love xoxo droid is awesome0
string.strip[[chars]] Parameter: There is only one optional parameter in it: 1]chars - a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string. Return Value: Returns a copy of the string with both leading and trailing characters removed.7
Output:
geeks for geeks for
xoxo love xoxo lov xoxo love xoxo droid is awesome8
string = ' xoxo love xoxo '
# Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left
# and right of string are removed
print[string.strip[' xoe']]
# Argument doesn't contain space
# No characters are removed.
print[string.strip['stx']]
string = 'android is awesome'
print[string.strip['an']]
9 string.strip[[chars]] Parameter: There is only one optional parameter in it: 1]chars - a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string. Return Value: Returns a copy of the string with both leading and trailing characters removed.0
We first construct a string str1 = ‘geeks for geeks’
Now we call strip method over str1 and pass str2 = ‘ekgs’ as argument.
Now python interpreter trace str1 from left.It remove the character of str1 if it is present in str2.
Otherwise it stops tracing.
Now python interpreter trace str1 from right. It remove the character of str1 if it is present in str2.
Otherwise it stop tracing.
Now at last it returns the resultant string.
When we call strip[] without argument, it removes leading and trailing spaces.
Python3
xoxo love xoxo lov xoxo love xoxo droid is awesome8
string = ' xoxo love xoxo '
# Leading and trailing whitespaces are removed
print[string.strip[]]
# All ,x,o,e characters in the left
# and right of string are removed
print[string.strip[' xoe']]
# Argument doesn't contain space
# No characters are removed.
print[string.strip['stx']]
string = 'android is awesome'
print[string.strip['an']]
9xoxo love xoxo lov xoxo love xoxo droid is awesome0
string.strip[[chars]] Parameter: There is only one optional parameter in it: 1]chars - a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string. Return Value: Returns a copy of the string with both leading and trailing characters removed.2
xoxo love xoxo lov xoxo love xoxo droid is awesome0
geeks for geeks geeks for geeks for3
Output:
geeks for geeks
print[string.strip[]] print[string.strip[' xoe']] print[string.strip['an']]string.strip[[chars]]
Parameter:
There is only one optional parameter in it:
1]chars - a string specifying
the set of characters to be removed.
If the optional chars parameter is not given, all leading
and trailing whitespaces are removed from the string.
Return Value:
Returns a copy of the string with both leading and trailing characters removed.
3
9 string = ' xoxo love xoxo '
# Leading and trailing whitespaces are removed
string.strip[[chars]]
Parameter:
There is only one optional parameter in it:
1]chars - a string specifying
the set of characters to be removed.
If the optional chars parameter is not given, all leading
and trailing whitespaces are removed from the string.
Return Value:
Returns a copy of the string with both leading and trailing characters removed.
5
Given a string remove occurrence of word “the” from the beginning and the end.
Hoạt động của mã trên: & nbsp; Đầu tiên chúng tôi xây dựng một chuỗi str1 = 'geek cho geek' & nbsp; Bây giờ chúng tôi gọi phương thức dải qua str1 và pass str2 = 'ekgs' làm đối số. & Nbsp; đặc tính của str1 nếu nó có mặt trong str2. & nbsp; nếu không thì nó dừng truy tìm. & nbsp; bây giờ là phiên dịch Python theo dõi str1 từ phải. Nó loại bỏ ký tự của str1 nếu nó có trong str2. & Nbsp; nếu không thì nó dừng truy tìm. & Nbsp; bây giờ cuối cùng, nó trả về chuỗi kết quả. Khi chúng ta gọi Dải [] mà không cần đối số, nó sẽ loại bỏ các không gian dẫn đầu và theo dõi. & NBSP; & NBSP ;
Ứng dụng thực tế: & nbsp; Cho một chuỗi xóa sự xuất hiện của Word, từ đầu và kết thúc. & Nbsp; & nbsp;
xoxo love xoxo lov xoxo love xoxo droid is awesome0
xoxo love xoxo lov xoxo love xoxo droid is awesome5
geeks for geeks geeks for geeks for9
xoxo love xoxo lov xoxo love xoxo droid is awesome7
Input:
the King has the largest army in the entire world the
Output:
King has the largest army in the entire world