Hướng dẫn what does rstrip () do in python? - rstrip () làm gì trong python?

Phương thức

string.rstrip([chars])
1 trả về một bản sao của chuỗi với các ký tự dấu vết bị xóa (dựa trên đối số chuỗi được truyền).

Thí dụ

title = 'Python Programming   '

# remove trailing whitespace from title result = title.rstrip() print(result)

# Output: Python Programming


Cú pháp của chuỗi rstrip ()

Cú pháp của

string.rstrip([chars])
1 là:

string.rstrip([chars])

tham số rstrip ()

  • Chars (Tùy chọn) - Một chuỗi chỉ định tập hợp các ký tự dấu sẽ được gỡ bỏ.

string.rstrip([chars])
1 loại bỏ các ký tự từ bên 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).

Nếu đối số chars không được cung cấp, tất cả các khoảng trắng ở bên phải sẽ bị xóa khỏi chuỗi.


Trả về giá trị từ rstrip ()

string.rstrip([chars])
1 trả về một bản sao của chuỗi với các ký tự dấu vết bị tước.

Tất cả các kết hợp của các ký tự trong đối số chars được loại bỏ khỏi bên phải của chuỗi cho đến khi sự không phù hợp đầu tiên.


Ví dụ: Làm việc của RSTRIP ()

random_string = 'this is good    '

# Trailing whitespace are removed
print(random_string.rstrip())

# 'si oo' are not trailing characters so nothing is removed
print(random_string.rstrip('si oo'))

# in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 

print(website.rstrip('m/.'))

Đầu ra

this is good
this is good
this is g
www.programiz.co

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

    Đọcreturns a copy of the string with trailing characters removed (based on the string argument passed). If no argument is passed, it removes trailing spaces.

    Bàn luận

    Phương thức Python String RStrip () trả về một bản sao của chuỗi với các ký tự dấu được xóa (dựa trên đối số chuỗi được truyền). Nếu không có đối số được thông qua, nó sẽ loại bỏ các không gian dấu vết.  string.rstrip([chars])

    Chuỗi python rstrip () Phương thức cú phápchars (optional) – a string specifying the set of characters to be removed.

    Cú pháp: & nbsp; string.rstrip ([chars])Returns a copy of the string with trailing characters stripped

    Tham số: chars (tùy chọn) - một chuỗi chỉ định tập hợp các ký tự sẽ bị xóa.

    Python3

    Trả về: Trả về một bản sao của chuỗi với các ký tự dấu vết bị tước

    string.rstrip([chars])
    8
    string.rstrip([chars])
    9
    random_string = 'this is good    '
    
    # Trailing whitespace are removed
    print(random_string.rstrip())
    
    # 'si oo' are not trailing characters so nothing is removed
    print(random_string.rstrip('si oo'))
    
    # in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
    print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 
    
    print(website.rstrip('m/.'))
    
    
    0
    random_string = 'this is good    '
    
    # Trailing whitespace are removed
    print(random_string.rstrip())
    
    # 'si oo' are not trailing characters so nothing is removed
    print(random_string.rstrip('si oo'))
    
    # in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
    print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 
    
    print(website.rstrip('m/.'))
    
    
    1

    Output:  

    geek

    Ví dụ Phương thức RSTRIP () Chuỗi Python

    string.rstrip([chars])
    5
    string.rstrip([chars])
    6
    string.rstrip([chars])
    7

    Python3

    string.rstrip([chars])
    5
    string.rstrip([chars])
    6
    random_string = 'this is good    '
    
    # Trailing whitespace are removed
    print(random_string.rstrip())
    
    # 'si oo' are not trailing characters so nothing is removed
    print(random_string.rstrip('si oo'))
    
    # in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
    print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 
    
    print(website.rstrip('m/.'))
    
    
    4

    string.rstrip([chars])
    8
    string.rstrip([chars])
    9
    random_string = 'this is good    '
    
    # Trailing whitespace are removed
    print(random_string.rstrip())
    
    # 'si oo' are not trailing characters so nothing is removed
    print(random_string.rstrip('si oo'))
    
    # in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
    print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 
    
    print(website.rstrip('m/.'))
    
    
    7
    random_string = 'this is good    '
    
    # Trailing whitespace are removed
    print(random_string.rstrip())
    
    # 'si oo' are not trailing characters so nothing is removed
    print(random_string.rstrip('si oo'))
    
    # in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
    print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 
    
    print(website.rstrip('m/.'))
    
    
    1

    Output:  

    geeks for g

    Ví dụ 2: Loại bỏ các không gian trắng theo dõi khỏi chuỗi bằng phương thức rstrip ()

    Python3

    string.rstrip([chars])
    5
    string.rstrip([chars])
    6
    this is good
    this is good
    this is g
    www.programiz.co
    1

    string.rstrip([chars])
    8
    this is good
    this is good
    this is g
    www.programiz.co
    3

    Output:  

    GeeksForGeeks

    Ví dụ 3: Chuỗi python rstrip () trả về chuỗi gốc nếu chuỗi không thể bị tước các ký tự được cung cấp

    Python3

    string.rstrip([chars])
    5
    string.rstrip([chars])
    6
    random_string = 'this is good    '
    
    # Trailing whitespace are removed
    print(random_string.rstrip())
    
    # 'si oo' are not trailing characters so nothing is removed
    print(random_string.rstrip('si oo'))
    
    # in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
    print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 
    
    print(website.rstrip('m/.'))
    
    
    4

    string.rstrip([chars])
    8
    string.rstrip([chars])
    9
    this is good
    this is good
    this is g
    www.programiz.co
    9
    random_string = 'this is good    '
    
    # Trailing whitespace are removed
    print(random_string.rstrip())
    
    # 'si oo' are not trailing characters so nothing is removed
    print(random_string.rstrip('si oo'))
    
    # in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
    print(random_string.rstrip('sid oo')) website = 'www.programiz.com/' 
    
    print(website.rstrip('m/.'))
    
    
    1

    Output:  

    geeks for geeks

    Phương thức

    string.rstrip([chars])
    1 trả về một bản sao của chuỗi bằng cách xóa các ký tự dấu được chỉ định là đối số. Nếu đối số ký tự không được cung cấp, tất cả các khoảng trắng kéo dài sẽ bị xóa khỏi chuỗi.

    Các ký tự dấu vết là những ký tự xảy ra ở cuối chuỗi (phần ngoài cùng của chuỗi).

    Syntax:

    str.rstrip(characters)

    Parameters:

    ký tự: (Tùy chọn) Một Char hoặc Chuỗi sẽ được xóa từ cuối chuỗi.

    Giá trị trở lại:

    Một chuỗi.

    Các ví dụ sau đây cho thấy phương pháp

    string.rstrip([chars])
    1.

    >>> mystr = '     Hello World     '
    >>> mystr.rstrip() # removes trailing spaces
    '     Hello World '
    >>> mystr = '''
    Python is 
    a programming language
    '''
    >>> mystr.rstrip()  # removes trailing \n
    '\nPython is \na programming language'
    >>> mystr = '----Hello World----'
    >>> mystr.rstrip('-')  # removes trailing -
    '----Hello World'
    

    Bạn có thể chỉ định một hoặc nhiều ký tự dưới dạng chuỗi để được xóa khỏi chuỗi theo bất kỳ thứ tự nào, như được hiển thị bên dưới.

    string.rstrip([chars])
    0

    Trong ví dụ trên,

    geek
    3 sẽ loại bỏ các chars '$', '2' hoặc '#' ngay cả khi xuất hiện theo bất kỳ thứ tự nào. Tuy nhiên,
    geek
    4 chỉ loại bỏ kéo dài '$' và '2', bởi vì '-' không được chỉ định để loại bỏ, vì vậy nó dừng ở đó và coi đó là một từ.

    Bạn muốn kiểm tra xem bạn biết Python bao nhiêu?

    Làm cách nào để sử dụng Rstrip trong Python?

    Phương thức Python String rstrip () Phương thức Python rstrip () Xóa tất cả các ký tự dấu khỏi chuỗi. Nó có nghĩa là nó loại bỏ tất cả các ký tự được chỉ định từ phía bên phải của chuỗi. Nếu chúng ta không chỉ định tham số, nó sẽ loại bỏ tất cả các khoảng trắng khỏi chuỗi. Phương thức này trả về một giá trị chuỗi.removes all the trailing characters from the string. It means it removes all the specified characters from right side of the string. If we don't specify the parameter, It removes all the whitespaces from the string. This method returns a string value.

    Rstrip và Lstrip làm gì trong Python?

    RSTRIP (): Trả về một chuỗi mới với khoảng trắng kéo dài.Nó dễ nhớ hơn khi loại bỏ các không gian trắng khỏi phía bên phải của chuỗi.LSTRIP (): Trả về một chuỗi mới với khoảng trắng hàng đầu bị loại bỏ hoặc loại bỏ khoảng trắng khỏi phía bên trái của chuỗi.returns a new string with trailing whitespace removed. It's easier to remember as removing white spaces from “right” side of the string. lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string.

    Hàm RStrip () có chứa bất kỳ đối số nào không?

    Phương thức rstrip () có một đối số tùy chọn chars.Đối số chars là một chuỗi chỉ định một tập hợp các ký tự mà phương thức rstrip () sẽ xóa khỏi bản sao của str.Nếu bạn bỏ qua đối số ký tự hoặc không sử dụng, đối số chars mặc định là các ký tự khoảng trắng. . The chars argument is a string that specifies a set of characters which the rstrip() method will remove from the copy of the str . If you omit the chars argument or use None , the chars argument defaults to whitespace characters.

    Làm cách nào để loại bỏ các không gian hàng đầu trong Python?

    Dải () Chức năng Dải chuỗi Python () sẽ loại bỏ các khoảng trắng dẫn đầu và dấu vết.Nếu bạn muốn loại bỏ các không gian dẫn hoặc dấu vết, hãy sử dụng hàm lstrip () hoặc rstrip () thay thế. Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.