1. Giới thiệu
Cũng giống như các ngôn ngữ khác trong Java, C#, C++... Thì python cũng có kiểu dữ liệu chuỗi.
Chuỗi là tập hợp các ký tự được xếp cạnh nhau. Được bao quanh bởi cặp dấu ngoặc kép hoặc cặp nháy đơn.
Chuỗi được sử dụng nhiều trong hầu hết các ngôn ngữ và python cũng không phải là ngoại lệ
Ví dụ: "machine learning" hay 'machine learning'
2. Các hàm xử lý chuỗi cơ bản
a. Truy xuất vào từng ký tự
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = 'Machine Learning' | |
print(s[2]) # c | |
# Truy xuất phần tử cuối | |
print(s[-1]) # g | |
# Truy xuất phần tử kề cuối | |
print(s[-2]) # n | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = 'Machine Learning' | |
print(s[::-1]) # gninraeL enihcaM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1 = 'Machine' | |
s2 = ' Learning' | |
s3 = s1 + s2 | |
print(s3) # Machine Learning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1 = 'Machine Learning' | |
count = s1.count('a', 0, len(s1)-1) # Đếm ký tự 'a' tính từ vị trí 0 kết thúc tại vị trí chiều dài của chuỗi-1. len(s1) để lấy chiều dài. | |
print(count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1 = 'Machine Learning Learning' | |
s2 = 'Learner' | |
s3 = s1.replace('Learning', s2, 1) # Learning -> Learner: 1 là số lượng từ tối đa có thể thay thế | |
print(s3) # Machine Learner Learning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1 = 'Machine Learning' | |
s2 = s1.find('chi', 0, len(s1)-1) # Tìm kiểu chuỗi 'chi' trong s1. Chỉ tìm kiếm trong khoảng từ 0 đến len(s1)-1. Không tìm thấy trả về -1 | |
print(s2) #2 |
g. Các hàm kiểm tra
- isalnum(): Hàm này kiểm tra các ký tự trong chuỗi có toàn là chữ và số hay không. Nếu có trả về true, ngược lại trả về false
- isalpha(): Hàm này kiểm tra các ký tự trong chuỗi có toàn là chữ hay không. Nếu có trả về true, ngược lại trả về false
- isdigit(): Hàm này kiểm tra các ký tự trong chuỗi có toàn là số hay không. Nếu có trả về true, ngược lại trả về false
- islower(): Hàm này kiểm tra các ký tự trong chuỗi in thường hay không. Nếu có trả về true, ngược lại trả về false
- isupper(): Hàm này kiểm tra các ký tự trong chuỗi in hoa hay không. Nếu có trả về true, ngược lại trả về false
0 nhận xét:
Đăng nhận xét