>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
1 [như trong các biểu thức chính quy] cho phép chia tách trên nhiều ký tự cùng một lúc:$ string = "blah, lots , of , spaces, here "
$ re.split[', ',string]
['blah', 'lots ', ' of ', ' spaces', 'here ']
Điều này không hoạt động tốt cho chuỗi ví dụ của bạn, nhưng hoạt động độc đáo cho danh sách phân tách không gian dấu phẩy. Đối với chuỗi ví dụ của bạn, bạn có thể kết hợp sức mạnh RE.Split để phân chia trên các mẫu regex để có được hiệu ứng "phân chia trên đó hoặc đó".
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
Thật không may, đó là xấu xí, nhưng
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
2 sẽ thực hiện thủ thuật:$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
Voila!
Có hai tùy chọn để tìm một chuỗi con trong một chuỗi trong Python, Chuỗi tìm phương thức
3 và >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
4.>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
Mỗi người sẽ trả về vị trí mà chuỗi con được tìm thấy tại. Sự khác biệt giữa hai là
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
3 trả về vị trí thấp nhất và >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
4 trả về vị trí cao nhất.Các đối số bắt đầu và kết thúc tùy chọn có thể được cung cấp để giới hạn tìm kiếm chuỗi con trong các phần của chuỗi.
Example:
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
Nếu không tìm thấy chất nền, -1 được trả về.
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
Thêm thông tin:
Tài liệu Phương thức chuỗi.
Phương thức tham gia chuỗi
Phương thức
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
7 được sử dụng để tham gia tất cả các phần tử trong một >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
8 với một chuỗi được chỉ định >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
9. Nếu có thể xác định được bất kỳ giá trị không chuỗi nào, nó sẽ tăng ngoại lệ kiểu.>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
8: Tất cả các vòng lặp của chuỗi. Có thể một danh sách các chuỗi, tuple của chuỗi hoặc thậm chí là một chuỗi đơn giản.Ví dụ
Tham gia một chuỗi với
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
1print ":".join[["freeCodeCamp", "is", "fun"]]
Đầu ra
freeCodeCamp:is:fun
Tham gia một bộ chuỗi với
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
2print " and ".join[["A", "B", "C"]]
Đầu ra
A and B and C
Tham gia một bộ chuỗi với
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
2print " ".join["freeCodeCamp"]
Output:
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
0Chèn một
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
3 sau mỗi ký tự trong một chuỗi$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
1Output:
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
2Tham gia với chuỗi trống.
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
3Output:
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
4Thêm thông tin:
Tài liệu Phương thức chuỗi.
Phương thức tham gia chuỗi
Phương thức
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
7 được sử dụng để tham gia tất cả các phần tử trong một >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
8 với một chuỗi được chỉ định >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
9. Nếu có thể xác định được bất kỳ giá trị không chuỗi nào, nó sẽ tăng ngoại lệ kiểu.Ví dụ
- Tham gia một chuỗi với
1>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!" >>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string -1
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
5Đầu ra
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
6- Tham gia một bộ chuỗi với
2>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!" >>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string -1
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
7Đầu ra
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
8Thêm thông tin:
Tài liệu Phương thức chuỗi.
Phương thức tham gia chuỗi
Phương thức
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
7 được sử dụng để tham gia tất cả các phần tử trong một >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
8 với một chuỗi được chỉ định >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
9. Nếu có thể xác định được bất kỳ giá trị không chuỗi nào, nó sẽ tăng ngoại lệ kiểu.>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
8: Tất cả các vòng lặp của chuỗi. Có thể một danh sách các chuỗi, tuple của chuỗi hoặc thậm chí là một chuỗi đơn giản.Example:
$ re.split['[, ]',string]
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
9Ví dụ
Tham gia một chuỗi với>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
1Đầu ra
Tham gia một bộ chuỗi với >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
2
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
Chèn một
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
3 sau mỗi ký tự trong một chuỗiTham gia với chuỗi trống.
Tham gia với các bộ.
Tài liệu Python trên Chuỗi Tham gia
Phương thức thay thế chuỗi
Phương pháp
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
4 được sử dụng để thay thế chuỗi con >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
5 bằng chuỗi >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
6 với tổng số >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
7 lần. Phương thức này trả về một bản sao mới của chuỗi với sự thay thế. Chuỗi ban đầu >>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
9 không thay đổi.Ví dụ
Tham gia một chuỗi với
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
1$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
1Output:
$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
2Đầu ra
$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
3Output:
$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
4Tham gia một bộ chuỗi với
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
2$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
5Output:
$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
6Chèn một
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you', 43] # find 'you' in string anywhere from position 43 to the end of the string
-1
3 sau mỗi ký tự trong một chuỗiall whitespace$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
7Output:
$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
6Tham gia với chuỗi trống.
$ filter[None, re.split['[, ]',string]]
['blah', 'lots', 'of', 'spaces', 'here']
9Output:
>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!"
>>> string.find['you']
6
>>> string.rfind['you']
42
0Tham gia với các bộ.
Tài liệu Python trên Chuỗi Tham gia
Phương thức thay thế chuỗi