Hướng dẫn how do you set the end of a string in python? - làm thế nào để bạn đặt phần cuối của một chuỗi trong python?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức chuỗi python () với sự trợ giúp của các ví dụ.

Phương thức

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
5 trả về
text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
6 nếu một chuỗi kết thúc với hậu tố được chỉ định. Nếu không, nó trả về
text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
7.

Thí dụ

message = 'Python is fun'

# check if the message ends with fun print(message.endswith('fun'))

# Output: True


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

Cú pháp của

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
5 là:

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

Các tham số endswith ()

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
5 có ba tham số:

  • Hậu tố - Chuỗi hoặc bộ tải hậu tố cần kiểm tra - String or tuple of suffixes to be checked
  • Bắt đầu (Tùy chọn) - Vị trí bắt đầu trong đó hậu tố sẽ được kiểm tra trong chuỗi. (optional) - Beginning position where suffix is to be checked within the string.
  • Kết thúc (Tùy chọn) - Vị trí kết thúc trong đó hậu tố sẽ được kiểm tra trong chuỗi. (optional) - Ending position where suffix is to be checked within the string.

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

Phương pháp

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
5 trả về một boolean.

  • Nó trả về true nếu một chuỗi kết thúc với hậu tố được chỉ định.
  • Nó trả về sai nếu một chuỗi không kết thúc với hậu tố được chỉ định.

Ví dụ 1: endswith () không có tham số bắt đầu và kết thúc

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)

Đầu ra

False
True
True

Ví dụ 2: endswith () với tham số bắt đầu và kết thúc

text = "Python programming is easy to learn."

# start parameter: 7
# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False print(result)

result = text.endswith('easy', 7, 26)

# returns True print(result)

Đầu ra

True
False
True

Ví dụ 2: endswith () với tham số bắt đầu và kết thúc

Chuyển tuple cho endswith ()

Có thể truyền một hậu tố tuple cho phương thức

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
5 trong Python.


Nếu chuỗi kết thúc bằng bất kỳ mục nào của tuple, text = "Python is easy to learn." result = text.endswith('to learn') # returns False print(result) result = text.endswith('to learn.') # returns True print(result) result = text.endswith('Python is easy to learn.') # returns True print(result)5 sẽ trả về đúng. Nếu không, nó trả về sai

text = "programming is easy"

result = text.endswith(('programming', 'python'))

# prints False print(result)

result = text.endswith(('python', 'easy', 'java'))

#prints True print(result) # With start and end parameter # 'programming is' string is checked

result = text.endswith(('is', 'an'), 0, 14)

# prints True print(result)

Đầu ra

False
True
True

Ví dụ 2: endswith () với tham số bắt đầu và kết thúc

Có một cách để xử lý chức năng

False
True
True
3 để thực hiện là hoạt động ngược. Ý tưởng là đọc chuỗi, văn bản để thay thế và văn bản mới từ đầu đến đầu. Logic vẫn giữ nguyên.

Điều này dựa trên cách chuyển một chuỗi ngược bằng

False
True
True
4.

Ví dụ:

>>> a = "123 456 123 456 123 456"
>>> print(a)
123 456 123 456 123 456

và chúng tôi muốn thay thế một lần cuối cùng, (hoặc hai, hoặc n) xảy ra của "123" bằng "ABC"

>>> res = a[::-1].replace("123"[::-1], "abc"[::-1], 1)[::-1]
>>> print(res)
123 456 123 456 abc 456

Điều này có thể được đặt như một chức năng hoạt động chính xác như thay thế.

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

Execution:

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

Hướng dẫn how do you set the end of a string in python? - làm thế nào để bạn đặt phần cuối của một chuỗi trong python?

Python cung cấp nhiều cách để tạo chuỗi một chuỗi. Điều này thường được gọi là "cắt lát".

Đây là cú pháp:

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

Where,

False
True
True
5: Chỉ số bắt đầu của chuỗi con. Nhân vật tại chỉ số này được bao gồm trong chuỗi con. Nếu
False
True
True
5 không được bao gồm, nó được giả định bằng 0.

False
True
True
7: Chỉ số chấm dứt của chuỗi con. Nhân vật tại chỉ số này không được bao gồm trong chuỗi con. Nếu
False
True
True
7 không được bao gồm hoặc nếu giá trị được chỉ định vượt quá độ dài chuỗi, nó được giả định là bằng chiều dài của chuỗi theo mặc định.

False
True
True
9: Mỗi ký tự "bước" sau khi ký tự hiện tại được đưa vào. Giá trị mặc định là 1. Nếu không bao gồm
False
True
True
9, nó được giả định là bằng 1.

Cách sử dụng cơ bản

text = "Python programming is easy to learn."

# start parameter: 7
# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False print(result)

result = text.endswith('easy', 7, 26)

# returns True print(result)
1: Nhận tất cả các ký tự từ
False
True
True
5 đến
False
True
True
7 - 1

text = "Python programming is easy to learn."

# start parameter: 7
# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False print(result)

result = text.endswith('easy', 7, 26)

# returns True print(result)
4: Nhận tất cả các ký tự từ đầu chuỗi đến
False
True
True
7 - 1

text = "Python programming is easy to learn."

# start parameter: 7
# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False print(result)

result = text.endswith('easy', 7, 26)

# returns True print(result)
6: Nhận tất cả các ký tự từ
False
True
True
5 đến cuối chuỗi

text = "Python programming is easy to learn."

# start parameter: 7
# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False print(result)

result = text.endswith('easy', 7, 26)

# returns True print(result)
8: Nhận tất cả các ký tự từ
False
True
True
5 đến
False
True
True
7 - 1, không bao gồm mọi ký tự
False
True
True
9

Ví dụ

1. Nhận 5 ký tự đầu tiên của chuỗiGet the first 5 characters of a string

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

Output:

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

Lưu ý:

True
False
True
2 Trả về kết quả tương tự như
True
False
True
3

2. Lấy một chuỗi con dài 4 ký tự, bắt đầu từ ký tự thứ 3 của chuỗiGet a substring 4 characters long,starting from the 3rd character of the string

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

Output:

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

3. Nhận ký tự cuối cùng của chuỗiGet the last character of the string

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

Output:

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

Lưu ý rằng chỉ số

False
True
True
5 hoặc
False
True
True
7 có thể là một số âm. Một chỉ số âm có nghĩa là bạn bắt đầu đếm từ cuối chuỗi thay vì bắt đầu (từ bên phải sang trái).

Index -1 đại diện cho ký tự cuối cùng của chuỗi, -2 đại diện cho ký tự thứ hai đến cuối cùng, v.v.

4. Nhận 5 ký tự cuối cùng của chuỗiGet the last 5 characters of a string

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

Output:

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
0

5. Nhận một chuỗi con chứa tất cả các ký tự ngoại trừ 4 ký tự cuối cùng và ký tự thứ nhấtGet a substring which contains all characters except the last 4 characters and the 1st character

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
1

Output:

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
2

6. Nhận mọi ký tự khác từ một chuỗiGet every other character from a string

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
3

Output:

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)
4

Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Làm thế nào để bạn thay đổi kết thúc của một chuỗi trong Python?

Trong Python, phương thức .replace () và hàm re.sub () thường được sử dụng để làm sạch văn bản bằng cách loại bỏ các chuỗi hoặc chuỗi con hoặc thay thế chúng.the . replace() method and the re. sub() function are often used to clean up text by removing strings or substrings or replacing them.

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.

Cái gì [

Python cũng cho phép bạn lập chỉ mục từ cuối danh sách bằng số âm, trong đó [-1] trả về phần tử cuối cùng.Điều này là siêu hữu dụng vì nó có nghĩa là bạn không phải lập trình tìm ra độ dài của điều đáng tin cậy để làm việc với các yếu tố ở cuối của nó.returns the last element. This is super-useful since it means you don't have to programmatically find out the length of the iterable in order to work with elements at the end of it.

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

Việc cắt bắt đầu với chỉ mục start_pos (bao gồm) và kết thúc tại end_pos index (loại trừ).Tham số bước được sử dụng để chỉ định các bước cần thực hiện từ chỉ mục từ đầu đến cuối.Cắt chuỗi python luôn tuân theo quy tắc này: s [: i] + s [i:] == s cho bất kỳ chỉ mục 'i'.s[:i] + s[i:] == s for any index 'i'.