Hướng dẫn how do you end a string in python? - làm thế nào để bạn kết thúc một chuỗi trong python?



Sự mô tả

Phương thức chuỗi python endswith () trả về true nếu chuỗi kết thúc bằng hậu tố được chỉ định, nếu không hãy trả về sai tùy chọn hạn chế khớp với các chỉ số đã cho bắt đầu và kết thúc.endswith() returns True if the string ends with the specified suffix, otherwise return False optionally restricting the matching with the given indices start and end.

Cú pháp

str.endswith(suffix[, start[, end]])

Thông số

  • Hậu tố - Đây có thể là một chuỗi hoặc cũng có thể là một bộ hậu tố để tìm kiếm. − This could be a string or could also be a tuple of suffixes to look for.

  • Bắt đầu - lát cắt bắt đầu từ đây. − The slice begins from here.

  • Kết thúc - các lát kết thúc ở đây. − The slice ends here.

Giá trị trả về

Đúng nếu chuỗi kết thúc với hậu tố được chỉ định, nếu không là sai.

Thí dụ

#!/usr/bin/python

str = "this is string example....wow!!!";

suffix = "wow!!!";
print str.endswith(suffix)
print str.endswith(suffix,20)

suffix = "is";
print str.endswith(suffix, 2, 4)
print str.endswith(suffix, 2, 6)

Kết quả

True
True
True
False

python_strings.htm

Nhân vật dòng mới trong Python là \ n. Nó được sử dụng để chỉ ra sự kết thúc của một dòng văn bản.

Làm thế nào để bạn tìm thấy kết thúc của một chuỗi?

Phương thức EndSwith () xác định xem một chuỗi kết thúc bằng các ký tự của chuỗi được chỉ định, trả về đúng hay sai khi thích hợp.

  • Xem thảo luận
  • Cải thiện bài viết
  • Nhân vật dòng mới trong Python là \ n. Nó được sử dụng để chỉ ra sự kết thúc của một dòng văn bản.

    Làm thế nào để bạn tìm thấy kết thúc của một chuỗi?

    Phương thức EndSwith () xác định xem một chuỗi kết thúc bằng các ký tự của chuỗi được chỉ định, trả về đúng hay sai khi thích hợp.

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Python3

    Đọc

    Bàn luận

    Output:

    Welcome to GeeksforGeeks

    Theo mặc định, hàm print () của Python kết thúc bằng một dòng mới. Một lập trình viên có nền C/C ++ có thể tự hỏi làm thế nào để in mà không có dòng mới. Chức năng Python Python Print () đi kèm với một tham số gọi là ‘kết thúc. Theo mặc định, giá trị của tham số này là ‘\ n, tức là ký tự dòng mới. & Nbsp;

    Ví dụ 1:

    Python3

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    Welcome to GeeksforGeeks
    1
    True
    True
    True
    False
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    9
    Welcome to GeeksforGeeks
    4
    True
    True
    True
    False
    
    1

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    True
    True
    True
    False
    
    4
    True
    True
    True
    False
    
    1

    Output:

    Python@GeeksforGeeks

    Ở đây, chúng ta có thể kết thúc một câu lệnh in với bất kỳ ký tự/chuỗi nào bằng tham số này. & NBSP;

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    7
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    8
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    9
    True
    True
    True
    False
    
    0
    True
    True
    True
    False
    
    1

    Python3

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    True
    True
    True
    False
    
    4
    True
    True
    True
    False
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    9
    True
    True
    True
    False
    
    0
    True
    True
    True
    False
    
    1

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    Python@GeeksforGeeks
    2
    True
    True
    True
    False
    
    1

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    GFG
    09-12-2016
    Red,Green,Blue@geeksforgeeks
    6
    Python@GeeksforGeeks
    3
    GFG
    09-12-2016
    Red,Green,Blue@geeksforgeeks
    8
    Python@GeeksforGeeks
    3
    word = "Help"
    word[1:-1]  # But I want to grab up to end of string!
    word[1:len(word)]  # Works but is there anything better?
    
    0
    Python@GeeksforGeeks
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    9
    word = "Help"
    word[1:-1]  # But I want to grab up to end of string!
    word[1:len(word)]  # Works but is there anything better?
    
    3
    True
    True
    True
    False
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    9
    word = "Help"
    word[1:-1]  # But I want to grab up to end of string!
    word[1:len(word)]  # Works but is there anything better?
    
    6
    True
    True
    True
    False
    
    1

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    word[1:]
    
    0
    Python@GeeksforGeeks
    3
    word[1:]
    
    2
    Python@GeeksforGeeks
    3
    word[1:]
    
    4
    Python@GeeksforGeeks
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    9
    word[1:]
    
    7
    True
    True
    True
    False
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    9
    Welcome to GeeksforGeeks
    4
    True
    True
    True
    False
    
    1

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    5
    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    6
    >>> word = "Help"
    >>> word[-3:]
    'elp'
    
    4
    True
    True
    True
    False
    
    1

    Ví dụ 2: & nbsp;

    GFG
    09-12-2016
    Red,Green,Blue@geeksforgeeks

    Phương thức chuỗi


    Thí dụ

    Kiểm tra xem chuỗi kết thúc bằng dấu chấm câu (.):

    TXT = "Xin chào, Chào mừng bạn đến với thế giới của tôi."

    x = txt.endswith (".")

    print(x)

    Hãy tự mình thử »


    Định nghĩa và cách sử dụng

    Phương thức

    >>> word = "Help"
    >>> word[-3:]
    'elp'
    
    6 trả về true nếu chuỗi kết thúc bằng giá trị được chỉ định, nếu không thì sai.


    Cú pháp

    String.endswith (giá trị, bắt đầu, kết thúc)

    Giá trị tham số

    Tham sốSự mô tả
    giá trịYêu cầu. Giá trị để kiểm tra xem chuỗi kết thúc với
    bắt đầuKhông bắt buộc. Một số nguyên chỉ định tại vị trí bắt đầu tìm kiếm
    chấm dứtKhông bắt buộc. Một số nguyên chỉ định tại vị trí để kết thúc tìm kiếm

    Nhiều ví dụ hơn

    Thí dụ

    Kiểm tra xem chuỗi kết thúc bằng dấu chấm câu (.):

    TXT = "Xin chào, Chào mừng bạn đến với thế giới của tôi."

    x = txt.endswith (".")

    Hãy tự mình thử »

    Hãy tự mình thử »

    Thí dụ

    Kiểm tra xem chuỗi kết thúc bằng dấu chấm câu (.):

    TXT = "Xin chào, Chào mừng bạn đến với thế giới của tôi."

    x = txt.endswith (".")

    Hãy tự mình thử »

    Hãy tự mình thử »


    Phương thức chuỗi


    33

    Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
    Learn more.

    Với các chỉ số chuỗi, có cách nào để cắt vào cuối chuỗi mà không cần sử dụng

    >>> word = "Help"
    >>> word[-3:]
    'elp'
    
    7 không?

    Các chỉ số tiêu cực bắt đầu từ cuối, nhưng

    >>> word = "Help"
    >>> word[-3:]
    'elp'
    
    8 bỏ qua ký tự cuối cùng.

    word = "Help"
    word[1:-1]  # But I want to grab up to end of string!
    word[1:len(word)]  # Works but is there anything better?
    

    Hướng dẫn how do you end a string in python? - làm thế nào để bạn kết thúc một chuỗi trong python?

    mkrieger1

    15.9k4 Huy hiệu vàng47 Huy hiệu bạc57 Huy hiệu Đồng4 gold badges47 silver badges57 bronze badges

    hỏi ngày 18 tháng 3 năm 2012 lúc 9:31Mar 18, 2012 at 9:31

    Hướng dẫn how do you end a string in python? - làm thế nào để bạn kết thúc một chuỗi trong python?

    3

    Thay vào đó bạn có thể thử sử dụng:

    word[1:]
    

    Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:33Mar 18, 2012 at 9:33

    Uku loskituku loskitUku Loskit

    39,8K9 Huy hiệu vàng88 Huy hiệu bạc92 Huy hiệu Đồng9 gold badges88 silver badges92 bronze badges

    0

    Hoặc thậm chí:

    >>> word = "Help"
    >>> word[-3:]
    'elp'
    

    Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:37Mar 18, 2012 at 9:37

    Hướng dẫn how do you end a string in python? - làm thế nào để bạn kết thúc một chuỗi trong python?

    Monkutmonkutmonkut

    40.4K23 Huy hiệu vàng120 Huy hiệu bạc148 Huy hiệu đồng23 gold badges120 silver badges148 bronze badges

    Tôi thấy mình cần chỉ định chỉ số cuối là một biến đầu vào trong một hàm. Trong trường hợp đó, bạn có thể thực hiện

    >>> word = "Help"
    >>> word[-3:]
    'elp'
    
    9. Ví dụ:

    def slice(val,start=1,stop=None)
        return val[start:stop]
    
    word = "Help"
    slice(word)  # output: 'elp'
    

    Đã trả lời ngày 30 tháng 4 năm 2016 lúc 21:29Apr 30, 2016 at 21:29

    Hướng dẫn how do you end a string in python? - làm thế nào để bạn kết thúc một chuỗi trong python?

    Bob Baxleybob BaxleyBob Baxley

    3,4451 Huy hiệu vàng21 Huy hiệu bạc28 Huy hiệu đồng1 gold badge21 silver badges28 bronze badges

    1

    Vâng, tất nhiên, bạn nên:

    word[1:]
    

    Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:34Mar 18, 2012 at 9:34

    DenisdenisDenis

    6.7397 Huy hiệu vàng37 Huy hiệu bạc58 Huy hiệu Đồng7 gold badges37 silver badges58 bronze badges

    Bạn đang tìm kiếm điều này?

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    1

    Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:34Mar 18, 2012 at 9:34

    DenisdenisRik Poggi

    6.7397 Huy hiệu vàng37 Huy hiệu bạc58 Huy hiệu Đồng6 gold badges63 silver badges81 bronze badges

    Bạn đang tìm kiếm điều này?

    word[1:]
    

    Ở đây bạn đang chỉ định rằng bạn muốn các ký tự từ INDEX 1, đây là ký tự thứ hai của chuỗi của bạn, cho đến khi chỉ mục cuối cùng ở cuối. Điều này có nghĩa là bạn chỉ cắt ký tự ở chỉ mục đầu tiên của chuỗi, trong trường hợp này là 'H'. In ấn này sẽ dẫn đến: 'ELP'

    Không chắc đó có phải là những gì bạn đã theo đuổi.

    Đã trả lời ngày 28 tháng 4 năm 2019 lúc 19:15Apr 28, 2019 at 19:15

    Đôi khi rất hữu ích để lưu chỉ mục, ví dụ: sử dụng nó trên các biến khác hoặc chuyển nó đến các chức năng. Ở đây, các đối tượng

    def slice(val,start=1,stop=None)
        return val[start:stop]
    
    word = "Help"
    slice(word)  # output: 'elp'
    
    0 có hữu ích.

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    3

    Đã trả lời ngày 27 tháng 5 năm 2021 lúc 16:45May 27, 2021 at 16:45

    Japamatjapamatjapamat

    5454 Huy hiệu bạc5 Huy hiệu Đồng4 silver badges5 bronze badges

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    
    suffix = "wow!!!";
    print str.endswith(suffix)
    print str.endswith(suffix,20)
    
    suffix = "is";
    print str.endswith(suffix, 2, 4)
    print str.endswith(suffix, 2, 6)
    
    4

    'Help'

    Tôi hy vọng cái này sẽ giúp bạn

    Hướng dẫn how do you end a string in python? - làm thế nào để bạn kết thúc một chuỗi trong python?

    Tunaki

    128K45 Huy hiệu vàng323 Huy hiệu bạc406 Huy hiệu Đồng45 gold badges323 silver badges406 bronze badges

    Đã trả lời ngày 3 tháng 6 năm 2016 lúc 10:11Jun 3, 2016 at 10:11

    Kết thúc = 'làm gì trong Python?

    Tham số cuối trong hàm in được sử dụng để thêm bất kỳ chuỗi nào.Ở cuối đầu ra của câu lệnh in trong Python.Theo mặc định, chức năng in kết thúc bằng một dòng mới.Chuyển khoảng trắng sang tham số cuối (end = '') chỉ ra rằng ký tự cuối phải được xác định bằng khoảng trắng chứ không phải là dòng mới.add any string. At the end of the output of the print statement in python. By default, the print function ends with a newline. Passing the whitespace to the end parameter (end=' ') indicates that the end character has to be identified by whitespace and not a newline.

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

    Sử dụng phương thức .Strip () để loại bỏ khoảng trắng và các ký tự từ đầu và phần cuối của chuỗi.Sử dụng .Phương thức lStrip () để loại bỏ khoảng trắng và ký tự chỉ từ đầu chuỗi. strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.

    Làm thế nào để bạn kết thúc một dòng trong Python?

    Nhân vật dòng mới trong Python là \ n.Nó được sử dụng để chỉ ra sự kết thúc của một dòng văn bản.\n . It is used to indicate the end of a line of text.

    Làm thế nào để bạn tìm thấy kết thúc của một chuỗi?

    Phương thức EndSwith () xác định xem một chuỗi kết thúc bằng các ký tự của chuỗi được chỉ định, trả về đúng hay sai khi thích hợp.endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.