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
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/.']]
0random_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/.']]
1Output:
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/.']]
4string.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/.']]
7random_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/.']]
1Output:
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.co1
string.rstrip[[chars]]8
this is good this is good this is g www.programiz.co3
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/.']]
4string.rstrip[[chars]]8
string.rstrip[[chars]]9
this is good this is good this is g www.programiz.co9
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/.']]
1Output:
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,
geek3 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,
geek4 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?