Làm cách nào để khắc phục sự cố chuỗi trong python?

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!
1

5. Đổi tên hàng loạt tệp

Mã số

Hello World!
Hello World!
2

đầu ra

Hello World!
Hello World!
3

6. đoán từ

Mã số

Hello World!
Hello World!
4

đầu ra

Hello World!
Hello World!
5

7. Đả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!
7

số 8. Đảo ngược một chuỗi

Mã số

Hello World!
Hello World!
8

đầu ra

Hello World!
Hello World!
9

9. 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[]
1

10. 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[]
2

Trong [ ]

#import library
import os
#Get a list of files in the current directory
os.listdir[]
3

Trong [ ]

#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[]
5

11. Xoay một chuỗi

Mã số

#import library
import os
#Get a list of files in the current directory
os.listdir[]
6

Trong [ ]

#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[]
8

12. 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']
0

13. 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']
1

14. 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']
3

15. 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']
5

16. 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']
7

17. 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']
9

18. 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[]
1

19. 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']
3

20. 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[]
5

21. 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[]
7

Mã số

#Find out what the current directory is
os.getcwd[]
8

đầu ra

#Find out what the current directory is
os.getcwd[]
9

22. nối chuỗi

Mã số

'/content'
0

đầu ra

'/content'
1

23. Đếm Số Nguyên Âm Trong Một Chuỗi

Mã số

'/content'
2

đầu ra

'/content'
3

24. Chuyển đổi một chuỗi thành một từ điển

Mã số

'/content'
4

Trong [ ]

'/content'
5

Trong [ ]

'/content'
6

Trong [ ]

'/content'
7

đầu ra

'/content'
8

25. 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]
0

26. 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ạn

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]
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]
4

28. 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]
0

29. 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]
0

30. 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 : Hello
path : /content
0

31. Chuyển đổi một chuỗi thành một int hoặc float

Mã số

getfiles[path]input text : Hello
path : /content
1

đầu ra

getfiles[path]input text : Hello
path : /content
2

32. Tạo một bài thơ

Mã số

getfiles[path]input text : Hello
path : /content
3

đầu ra

getfiles[path]input text : Hello
path : /content
433. Đọc thông tin từ một trang Wikipedia

Mã số

getfiles[path]input text : Hello
path : /content
5

Trong [ ]

getfiles[path]input text : Hello
path : /content
6

Trong [ ]

getfiles[path]input text : Hello
path : /content
7

đầ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 : Hello
path : /content
8

đầu ra

getfiles[path]input text : Hello
path : /content
9

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 Libraries
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]
0

đầu ra

#import Libraries
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]
1

36. Trực quan hóa dữ liệu chuỗi

Mã số

#import Libraries
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]
2

đầ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 Libraries
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]
3

đầu ra

#import Libraries
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]
4

38. Trả về chỉ số cao nhất của chuỗi

Mã số

#import Libraries
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]
5

đầu ra

#import Libraries
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]
6

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 Libraries
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]
7

đầu ra

#import Libraries
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]
8

40. Sử dụng Encode[] để mã hóa một chuỗi

Mã số

#import Libraries
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]
9

đầu ra

IWKsv0?M%PS$6cZ[
0

Mã số

IWKsv0?M%PS$6cZ[
1

đầu ra

IWKsv0?M%PS$6cZ[
2

41. 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[
4

42. 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[
6

43. Chỉ định khoảng trắng trong chuỗi

Mã số

IWKsv0?M%PS$6cZ[
7

đầu ra

IWKsv0?M%PS$6cZ[
8

44. 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!
00

45. Thay đổi kiểu dữ liệu thành chuỗi

Mã số

Hello World!
Hello World!
01

đầu ra

Hello World!
Hello World!
02

Mã số

Hello World!
Hello World!
03

đầu ra

Hello World!
Hello World!
04

46. 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!
06

47. 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!
08

48. 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!
10

49. 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!
12

50. 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!
14

Cả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. 😃

Python xử lý dữ liệu chuỗi như thế nào?

Trong Python, các chuỗi được sắp xếp theo thứ tự các chuỗi dữ liệu ký tự và do đó có thể được lập chỉ mục theo cách này. Có thể truy cập từng ký tự trong một chuỗi bằng cách chỉ định tên chuỗi theo sau là một số trong dấu ngoặc vuông [ [] ] .

Các chuỗi trong Python có thể được sửa đổi không?

Chuỗi Python là "bất biến", nghĩa là không thể thay đổi chúng sau khi tạo [Chuỗi Java cũng sử dụng kiểu bất biến này]. Vì các chuỗi không thể thay đổi, nên chúng tôi xây dựng các chuỗi *new* khi chúng tôi biểu diễn các giá trị được tính toán.

Lệnh cho chuỗi trong Python là gì?

Nếu bạn muốn thực thi các câu lệnh Python, bạn có thể sử dụng exec[string] . Ví dụ: >>> my_code = 'print "Xin chào thế giới. "' >>> exec[my_code] Hello World.

Các cách khác nhau để thực hiện định dạng chuỗi trong Python là gì?

Có bốn cách khác nhau để thực hiện định dạng chuỗi trong Python. .
Định dạng với toán tử %
Định dạng bằng phương thức chuỗi format[]
Định dạng bằng chuỗi ký tự, được gọi là chuỗi f
Định dạng với Lớp mẫu chuỗi

Chủ Đề