Chuỗi là chuỗi các ký tự. Tên của bạn có thể được coi là một chuỗi. Hoặc, giả sử bạn sống ở Zambia, sau đó tên quốc gia của bạn là
>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
4, đó là một chuỗi.Trong hướng dẫn này, bạn sẽ thấy các chuỗi được xử lý bằng Python như thế nào, các cách khác nhau trong đó các chuỗi được thể hiện bằng Python và cách bạn có thể sử dụng các chuỗi trong mã của mình.
Cách tạo một chuỗi và gán nó cho một biến
Để tạo một chuỗi, hãy đặt chuỗi các ký tự bên trong các trích dẫn đơn, dấu ngoặc kép hoặc trích dẫn ba và sau đó gán nó cho một biến. Bạn có thể xem xét cách các biến hoạt động trong Python trong hướng dẫn biến Python.
Ví dụ: bạn có thể gán một ký tự ’a cho một biến
>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
5. Lưu ý rằng chuỗi là một ký tự duy nhất và nó được đặt kèm theo các trích dẫn đơn lẻ.>>> single_quote_character = 'a'
>>> print[single_quote_character]
a
>>> print[type[single_quote_character]] # check the type of the variable.
Tương tự, bạn có thể gán một ký tự duy nhất cho một biến
>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
6. Lưu ý rằng chuỗi là một ký tự duy nhất nhưng nó được đặt kèm theo các trích dẫn kép.>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
Ngoài ra, hãy xem nếu bạn có thể gán một chuỗi các ký tự hoặc nhiều ký tự cho một biến. Bạn có thể gán cả hai chuỗi trích dẫn đơn và chuỗi trích dẫn kép.
>>> double_quote_multiple_characters = "aeiou"
>>> single_quote_multiple_characters = 'aeiou'
>>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
Thật thú vị nếu bạn kiểm tra sự tương đương của cái này sang cái kia bằng cách sử dụng từ khóa
>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
7, nó sẽ trả về đúng.>>> print[double_quote_multiple_characters is double_quote_multiple_characters]
True
Hãy xem bài tập của các chuỗi bằng cách sử dụng ba trích dẫn và kiểm tra xem chúng có thuộc lớp
>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
8 không.>>> triple_quote_example = """this is a sentence written in triple quotes"""
>>> print[type[triple_quote_example]]
Trong các ví dụ trên, hàm
>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
9 được sử dụng để hiển thị lớp bên dưới mà đối tượng sẽ thuộc về. Xin lưu ý rằng tất cả các biến đã được bắt đầu bằng trích dẫn đơn, gấp đôi hoặc ba được lấy dưới dạng chuỗi. Bạn có thể sử dụng trích dẫn đơn và đôi cho một dòng ký tự. Nhiều dòng thường được đặt trong trích dẫn ba.Chuỗi phương pháp phổ biến
- Nhận chỉ mục của một chuỗi con trong một chuỗi .________ 5
2 được trả về vì vị trí của các chữ cái riêng lẻ trong chuỗi là 0 chỉ số. Vì vậy, chỉ số của "A" trong "ABCDE" là 0, của "B" là 1, v.v.
Kiểm tra nếu một chuỗi con là thành viên của một chuỗi lớn hơn. Điều này được thực hiện bằng cách sử dụng từ khóa
0 và viết bài kiểm tra. Bộ xương được hiển thị dưới đây.>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
Subring trong chuỗi
>>> # for example, test if string "i" is present in string "pythonic" at least once. "i" is present in the string. Therefore, the result should be true. >>> "i" in "pythonic" True >>> # as "x" is not present in the string "pythonic" the below test should return false >>> "x" in "pythonic" # "x" is not present in "pythonic" False
Tham gia một danh sách các chuỗi bằng phương pháp tham gia. Một danh sách các chuỗi được viết bằng cách phân định chuỗi bằng dấu phẩy
1 và kèm theo cả nhóm với dấu ngoặc>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
2. Đối với một hướng dẫn chi tiết hơn về các danh sách hướng tới hướng dẫn danh sách Python. Bạn có thể tham gia một danh sách các chuỗi bằng cách đưa ra dấu phân cách là đối tượng mà phương thức>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
3 sẽ hoạt động và danh sách các chuỗi là đối số.>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
>>> # join a list of strings 1, 2, 3 with a space as a delimiter and 1,2,3 as the list of strings. So, the result will be the strings with spaces between them. >>> combined_string = " ".join[["1", "2", "3"]] '1 2 3'
Phá vỡ một chuỗi dựa trên một số quy tắc. Điều này lấy vào chuỗi làm đối tượng mà phương thức
4 được truyền bằng toán tử DOT. Tách mất một không gian làm tham số mặc định.>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
Ví dụ: bạn có thể phân chia một chuỗi dựa trên khoảng trống giữa các giá trị riêng lẻ.
>>> # split the string "1 2 3" and return a list of the numbers.
>>> "1 2 3".split[] # splitting
['1', '2', '3']
Hoặc bạn có thể phân chia một chuỗi dựa trên một dấu phân cách như
>>> double_quote_multiple_characters = "aeiou"
>>> single_quote_multiple_characters = 'aeiou'
>>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
5. >>> “1:2:3”.split[“:”]
[‘1’, ‘2’, ‘3’]
- Truy cập các ký tự riêng lẻ trong một chuỗi. Lưu ý phần tử đầu tiên có chỉ mục
6. Bạn truy cập phần tử đầu tiên với chỉ mục>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
6, phần tử thứ hai với chỉ mục>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
8, v.v.>>> double_quote_multiple_characters = "aeiou" >>> single_quote_multiple_characters = 'aeiou' >>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
Định dạng trong chuỗi:
Đối tượng chuỗi có thể được định dạng. Bạn có thể sử dụng
>>> double_quote_multiple_characters = "aeiou"
>>> single_quote_multiple_characters = 'aeiou'
>>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
9 như một định dạng sẽ cho phép bạn chèn các giá trị khác nhau vào một chuỗi trong thời gian chạy và do đó định dạng chuỗi. Biểu tượng >>> double_quote_multiple_characters = "aeiou"
>>> single_quote_multiple_characters = 'aeiou'
>>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
9 được thay thế bằng bất cứ điều gì được truyền vào chuỗi.>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
1Bạn cũng có thể sử dụng từ khóa
>>> print[double_quote_multiple_characters is double_quote_multiple_characters]
True
1. Điều này sẽ cho phép bạn đặt các loại hình dạng của riêng bạn thay vì >>> double_quote_multiple_characters = "aeiou"
>>> single_quote_multiple_characters = 'aeiou'
>>> print[type[double_quote_multiple_characters], type[single_quote_multiple_characters]]
9.>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
2Kiểm tra giá trị sự thật của chuỗi
Một chuỗi được coi là đúng trong Python nếu nó không phải là một chuỗi trống. Vì vậy, chúng tôi nhận được như sau:
>>> double_quote_character = "b"
>>> print[double_quote_character]
b
>>> print[type[double_quote_character]]
3Đóng góp bởi: Joydeep Bhattacharjee