Hướng dẫn how do you strip a value in python? - làm thế nào để bạn tách một giá trị trong python?

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,
    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'))

    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 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'))

    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 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'))

    2 - Vì
    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)
  • 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'))

    5 - Xóa
    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'))

    6 dẫn đầu chuỗi.

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ố

Tham sốSự mô tả
nhân vậtKhô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

  • Đọc
  • Bàn luận
  • 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'))

    8
    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

    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    0
    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    1

    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    0
    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    3

    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    0
    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    5
    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    6
    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    7

    Output:

        geeks for geeks     
    geeks for geeks
    for

    Python3

    Bàn luận

    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    0
    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 awesome
    0
    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 awesome
    8
    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 awesome
    8
    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

    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    0
    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 awesome
    0
        geeks for geeks     
    geeks for geeks
    for
    3

    Output:  

    geeks for geeks

    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
    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.
    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 awesome
    0
    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    5
        geeks for geeks     
    geeks for geeks
    for
    9
    xoxo love xoxo
    lov
      xoxo love xoxo   
    droid is awesome
    7

    Input:

    the King has the largest army in the entire world the

    Output: 

    King has the largest army in the entire world

    Làm thế nào để bạn tước văn bản trong Python?

    Python có ba phương pháp tích hợp để cắt tỉa khoảng trắng và dấu hiệu nào từ các chuỗi ...
    .strip().
    .lstrip().
    .rstrip().

    Dải trong Python có nghĩa là gì?

    Phương thức Dải () loại bỏ bất kỳ dấu dẫn 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ỏ)removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)

    Làm thế nào để bạn tước một phần của một chuỗi trong Python?

    Python loại bỏ ký tự khỏi chuỗi bằng cách sử dụng thay thế (), chúng ta có thể sử dụng chức năng String thay thế () để thay thế một ký tự bằng một ký tự mới.Nếu chúng tôi cung cấp một chuỗi trống làm đối số thứ hai, thì ký tự sẽ bị xóa khỏi chuỗi.use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

    Làm thế nào để bạn tước một mục từ một danh sách trong Python?

    Phương thức Remove () xóa phần tử khớp đầu tiên (được truyền dưới dạng đối số) khỏi danh sách.Phương thức pop () loại bỏ một phần tử tại một chỉ mục nhất định và cũng sẽ trả về mục đã xóa.Bạn cũng có thể sử dụng từ khóa DEL trong Python để xóa một phần tử hoặc lát khỏi danh sách.. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.