Chuỗi là tập hợp các bảng chữ cái, từ hoặc các ký tự khác. Nó là một trong những cấu trúc dữ liệu nguyên thủy và là khối xây dựng để thao tác dữ liệu. Python có một lớp chuỗi tích hợp có tên là str. Các chuỗi Python là “bất biến”, có nghĩa là chúng không thể thay đổi sau khi chúng được tạo
- Sejal Jaiswal, Trại dữ liệu
Danh sách này chứa các câu hỏi chuỗi thường gặp phổ biến nhất cho các cuộc phỏng vấn mã hóa. Các câu hỏi bao gồm các câu hỏi cơ bản cho đến các câu hỏi nâng cao hơn - không theo thứ tự cụ thể
Danh sách bài tập chuỗi Python
1. In một chuỗi
Mã số
#Message in single quotes
print['Hello World!']
#Message in double quotes
print["Hello World!"]
đầu ra
Hello World!
Hello World!
2. Tìm tệp bằng chuỗi
Mã số
#import library
import os
#Get a list of files in the current directory
os.listdir[]
đầu ra
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
Mã số
#Find out what the current directory is
os.getcwd[]
đầu ra
'/content'
Mã số
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
đầu ra
getfiles[path]input text : Hello
path : /content
3. Mã hóa một chuỗi để tạo mật khẩu
Mã số
________số 8đầu ra
IWKsv0?M%PS$6cZ[
4. Mã hóa tin nhắn bí mật
Mã số
Hello World!
Hello World!
0đầu ra
Hello World!
Hello World!
15. Đổi tên hàng loạt tệp
Mã số
Hello World!
Hello World!
2đầu ra
Hello World!
Hello World!
36. đoán từ
Mã số
Hello World!
Hello World!
4đầu ra
Hello World!
Hello World!
57. Đảo ngược một danh sách các chuỗi
Mã số
Hello World!
Hello World!
6đầu ra
Hello World!
Hello World!
7số 8. Đảo ngược một chuỗi
Mã số
Hello World!
Hello World!
8đầu ra
Hello World!
Hello World!
99. Trình tạo từ viết tắt
Mã số
#import library
import os
#Get a list of files in the current directory
os.listdir[]
0đầu ra
#import library
import os
#Get a list of files in the current directory
os.listdir[]
110. Tạo một câu chuyện
Mã số
#import library
import os
#Get a list of files in the current directory
os.listdir[]
2Trong [ ]
#import library
import os
#Get a list of files in the current directory
os.listdir[]
3Trong [ ]
#import library
import os
#Get a list of files in the current directory
os.listdir[]
4đầu ra
#import library
import os
#Get a list of files in the current directory
os.listdir[]
511. Xoay một chuỗi
Mã số
#import library
import os
#Get a list of files in the current directory
os.listdir[]
6Trong [ ]
#import library
import os
#Get a list of files in the current directory
os.listdir[]
7đầu ra
#import library
import os
#Get a list of files in the current directory
os.listdir[]
812. Sắp xếp một chuỗi
Mã số
#import library
import os
#Get a list of files in the current directory
os.listdir[]
9đầu ra
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
013. Tái tạo một câu chuyện bằng cách sử dụng định dạng và thay thế chuỗi
Mã số
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
114. Khớp một chuỗi
Mã số
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
2đầu ra
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
315. Trích xuất chuỗi giữa các thẻ HTML
Mã số
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
4đầu ra
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
516. Tìm chỉ số của các chuỗi con chồng chéo
Mã số
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
6đầu ra
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
717. Vòng qua một chuỗi
Mã số
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
8đầu ra
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
918. Tìm Độ Dài Của Một Chuỗi
Mã số
#Find out what the current directory is
os.getcwd[]
0đầu ra
#Find out what the current directory is
os.getcwd[]
119. Kiểm tra một chuỗi
Mã số
#Find out what the current directory is
os.getcwd[]
2đầu ra
['.config',
'Print a file location from a string.txt',
'Questions for interview.txt',
'sample_data']
320. cắt dây
Mã số
#Find out what the current directory is
os.getcwd[]
4đầu ra
#Find out what the current directory is
os.getcwd[]
521. Sửa đổi một chuỗi
Mã số
#Find out what the current directory is
os.getcwd[]
6đầu ra
#Find out what the current directory is
os.getcwd[]
7Mã số
#Find out what the current directory is
os.getcwd[]
8đầu ra
#Find out what the current directory is
os.getcwd[]
922. nối chuỗi
Mã số
'/content'
0đầu ra
'/content'
123. Đếm Số Nguyên Âm Trong Một Chuỗi
Mã số
'/content'
2đầu ra
'/content'
324. Chuyển đổi một chuỗi thành một từ điển
Mã số
'/content'
4Trong [ ]
'/content'
5Trong [ ]
'/content'
6Trong [ ]
'/content'
7đầu ra
'/content'
825. Kiểm tra xem một chuỗi có ít nhất một chữ cái hoặc số
Mã số
'/content'
9đầu ra
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
026. Xóa các khoảng trống trong một chuỗi
Mã số
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
1đầu ra
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
227 — Ghi lại mã của bạnMã số
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
3đầu ra
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
428. Tìm hiểu một chuỗi bắt đầu bằng chữ cái nào
Mã số
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
5đầu ra
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
029. Tìm hiểu một chuỗi kết thúc bằng chữ cái nào
Mã số
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
7đầu ra
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
030. Trả về một danh sách các chuỗi từ một chuỗi nhiều dòng
Mã số
#Assign text inputs to a variables
text = input["input text : "]
path = input["path : "]
#Create a definition that accepts the inputs and prints out the file location
def getfiles[path]:
f = 0
os.chdir[path]
files = os.listdir[]
# print[files]
for file_name in files:
abs_path = os.path.abspath[file_name]
if os.path.isdir[abs_path]:
getfiles[abs_path]
if os.path.isfile[abs_path]:
f = open[file_name, "r"]
if text in f.read[]:
f = 1
print[text + " found in "]
final_path = os.path.abspath[file_name]
print[final_path]
print['hello!']
return True
if f == 1:
print[text + " not found! "]
return False
getfiles[path]
9đầu ra
getfiles[path]input text : Hello0
path : /content
31. Chuyển đổi một chuỗi thành một int hoặc float
Mã số
getfiles[path]input text : Hello1
path : /content
đầu ra
getfiles[path]input text : Hello2
path : /content
32. Tạo một bài thơ
Mã số
getfiles[path]input text : Hello3
path : /content
đầu ra
getfiles[path]input text : Hello433. Đọc thông tin từ một trang Wikipedia
path : /content
Mã số
getfiles[path]input text : Hello5
path : /content
Trong [ ]
getfiles[path]input text : Hello6
path : /content
Trong [ ]
getfiles[path]input text : Hello7
path : /content
đầu ra
34 — Sử dụng Chuỗi để Sáng tạo Nghệ thuật [ASCII]
Mã đầy đủ có sẵn trên Github
Mã số
getfiles[path]input text : Hello8
path : /content
đầu ra
getfiles[path]input text : Hello9
path : /content
35. Tạo cơ sở dữ liệu
Đặt các thông tin sau vào một. txt và lưu cục bộ. Mỗi dòng chứa các thông tin sau. tên, tuổi, bộ phận, vai trò
Adam, 25, CNTT, Nhà khoa học dữ liệu Annie, 23, Tiếp thị, Giám đốc tiếp thị Ben, 28, CNTT, Phân tích dữ liệu Betty, 30, CNTT, CTO Charlie, 24, Tiếp thị, Chuyên gia tiếp thị Cathy, 32, CNTT, Phân tích dữ liệu
Mã số
#import Libraries0
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
đầu ra
#import Libraries1
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
36. Trực quan hóa dữ liệu chuỗi
Mã số
#import Libraries2
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
đầu ra
Nguồn. Hình ảnh của tác giả
37. Tạo Bot
Mã đầy đủ có sẵn trên Github/
Mã số
#import Libraries3
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
đầu ra
#import Libraries4
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
38. Trả về chỉ số cao nhất của chuỗi
Mã số
#import Libraries5
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
đầu ra
#import Libraries6
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
39. Trả lại một chuỗi sau khi thay thế sang bên trái hoặc bên phải
Mã số
#import Libraries7
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
đầu ra
#import Libraries8
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
40. Sử dụng Encode[] để mã hóa một chuỗi
Mã số
#import Libraries9
import random
import string#Create a randomly encrypted pasword
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join[random.sample[total, length]]
print[password]
đầu ra
IWKsv0?M%PS$6cZ[
0Mã số
IWKsv0?M%PS$6cZ[
1đầu ra
IWKsv0?M%PS$6cZ[
241. Pad Phía bên trái của Chuỗi có 0
Mã số
IWKsv0?M%PS$6cZ[
3đầu ra
IWKsv0?M%PS$6cZ[
442. Kiểm tra xem chuỗi có phải là mã định danh hợp lệ không
Mã số
IWKsv0?M%PS$6cZ[
5đầu ra
IWKsv0?M%PS$6cZ[
643. Chỉ định khoảng trắng trong chuỗi
Mã số
IWKsv0?M%PS$6cZ[
7đầu ra
IWKsv0?M%PS$6cZ[
844. Chuyển đổi một hình ảnh thành một chuỗi
Mã số
IWKsv0?M%PS$6cZ[
9đầu ra
Hello World!
Hello World!
0045. Thay đổi kiểu dữ liệu thành chuỗi
Mã số
Hello World!
Hello World!
01đầu ra
Hello World!
Hello World!
02Mã số
Hello World!
Hello World!
03đầu ra
Hello World!
Hello World!
0446. Loại bỏ các giá trị số khỏi một chuỗi
Mã số
Hello World!
Hello World!
05đầu ra
Hello World!
Hello World!
0647. Kiểm tra xem chuỗi có phải là từ khóa không
Mã số
Hello World!
Hello World!
07đầu ra
Hello World!
Hello World!
0848. Tìm các chuỗi có độ dài nhất định
Mã số
Hello World!
Hello World!
09đầu ra
Hello World!
Hello World!
1049. Xóa các bản sao liên tiếp trong một chuỗi
Mã số
Hello World!
Hello World!
11đầu ra
Hello World!
Hello World!
1250. Kiểm tra xem một chuỗi có phải là một Palindrome không
Mã số
Hello World!
Hello World!
13đầu ra
Hello World!
Hello World!
14Cảm ơn bạn đã đọc bài viết này
Ảnh của Jan Baborák trên Bapt
Bài viết được đề xuất
100 dự án cho người mới bắt đầu sử dụng Python. bởi Natasha Newbold. Tháng 4 năm 2022. Vừa phải
Trước khi bạn đi
Bạn cũng có thể ủng hộ tôi bằng cách chia sẻ bài viết của tôi, vỗ tay và bình luận. Khi bạn đăng ký thành viên trung bình, bạn sẽ có thể đọc vô số truyện của các nhà văn khác và của tôi. 💛
Tôi đang viết thêm nhiều câu chuyện và hướng dẫn về ngành dữ liệu. Bạn có thể mong đợi nhiều bài viết như thế này. Trong thời gian chờ đợi, vui lòng xem các bài viết khác của tôi
Cảm ơn vì đã đọc. Nếu bạn muốn liên hệ với tôi, vui lòng liên hệ với tôi trên Hồ sơ Linkedin của tôi. Bạn cũng có thể xem mã cho các bài viết trước trên Github của tôi. 😃