Tôi muốn làm như sau trong mã của mình:
- In từng từ của một chuỗi trong một dòng mới
- In từng ký tự trong một dòng mới
Tôi đã có thể đạt được phần thứ hai bằng cách sử dụng mã sau:
s2 = "I am testing"
for x in s2:
print x
Tôi đang gặp khó khăn trong việc đạt được phần đầu tiên. Tôi đã viết mã sau đây nhận ra nơi có không gian trong chuỗi.
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
Cũng đã thử các loại bên dưới trong đó có tất cả các không gian của chuỗi:
s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
Nhưng vẫn không đạt được đầu ra mong muốn của tôi, đó là một cái gì đó như:
I
am
testing
51 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
75for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
42for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
=
86__for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
51 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
75for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
42for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
=
86__for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
Làm cách nào để in một số từ nhất định từ một chuỗi trong Python?
Examples:
Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am
Cách tiếp cận: Chia chuỗi bằng hàm split []Finding even length wordsusing for loop and if statement and without using the def function. First split the given string using the split[] function and then iterate the words of a string using for loop. Calculate the length of each word using the len[] function. If the length is even, then print the word.
Python3
n
=
"This is a python language"
s
=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
3Làm cách nào để lấy một từ từ một chuỗi trong Python?
Phương pháp số 1: Sử dụng split [] Sử dụng hàm chia, chúng ta có thể chia chuỗi thành một danh sách các từ và đây là phương thức chung và được đề xuất nhất nếu người ta muốn hoàn thành nhiệm vụ cụ thể này. Nhưng nhược điểm là nó thất bại trong các trường hợp chuỗi chứa dấu chấm câu.
s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
8s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
1Làm thế nào để bạn hiển thị các từ trong Python?
This is python language
Trong Python, câu lệnh in được sử dụng để hiển thị văn bản. Trong Python với câu lệnh in, bạn có thể sử dụng trích dẫn đơn ['] hoặc trích dẫn kép ["]. Split the string using split[] function. Iterate in the words of a string using for loop. Calculate the length of the word using len[] function. If the length is even, then print the word. Below is the Python implementation of the above approach:
Python3
I
am
testing
1 I
am
testing
2s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
8I
am
testing
4=
I
am
testing
6I
am
testing
7for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
3s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
8for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
4 Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am1
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
6 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
7Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am4
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
9 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
0Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am7
s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
2s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
3=
____________s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
6s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
7This is python language4
s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am7
I
am
testing
4=
This is python language9
n
0
Phương pháp: Sử dụng chức năng Lambda
Python3
n
=
n
3
s
=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
3Các
s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9"This is a python language"
6Phương pháp: Sử dụng danh sách hiểu
Python3
n
=
n
3
s
=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
3s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9s
6for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
4 s
8for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
6 I
am
testing
4for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
9Phương pháp: Sử dụng hàm liệt kê & nbsp;
Python3
n
=
n
3
s
=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
3s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9s
6for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
4 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
21for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
6 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
23for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
24__Phương pháp số 1: Sử dụng split [] Sử dụng hàm chia, chúng ta có thể chia chuỗi thành một danh sách các từ và đây là phương thức chung và được đề xuất nhất nếu người ta muốn hoàn thành nhiệm vụ cụ thể này. Nhưng nhược điểm là nó thất bại trong các trường hợp chuỗi chứa dấu chấm câu.
Method #1 : Using split[]
Using the split function, we can split the string into a list of words and this is the most generic and recommended
method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.
Python3
Làm thế nào để bạn hiển thị các từ trong Python?
s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9 =
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
39 ________ 140 & nbsp; for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
41for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
42=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
63for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
64for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
65s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9 =
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
47 ________ 140 & nbsp; for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
49for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
50Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp;
The original string is : Geeksforgeeks is best Computer Science
Portal
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
& nbsp; & nbsp; Phương thức #3: Sử dụng regex [] + string.punning & nbsp; Phương pháp này cũng sử dụng các biểu thức chính quy, nhưng chức năng chuỗi của tất cả các dấu chấm câu được sử dụng để bỏ qua tất cả các dấu chấm câu và nhận chuỗi kết quả được lọc. & NBSP;
Method #2 : Using regex[ findall[] ]
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and
extracting words ignoring punctuation marks.
Python3
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
51 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
52for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
34=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
55s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9 =
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
39 ________ 140 & nbsp; for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
41for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
42=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
63for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
64for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
65s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9 =
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
47 ________ 140 & nbsp; for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
49for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
50Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp;
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is :
[‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
& nbsp; & nbsp; Phương thức #3: Sử dụng regex [] + string.punning & nbsp; Phương pháp này cũng sử dụng các biểu thức chính quy, nhưng chức năng chuỗi của tất cả các dấu chấm câu được sử dụng để bỏ qua tất cả các dấu chấm câu và nhận chuỗi kết quả được lọc. & NBSP;
Method #3 : Using regex[] + string.punctuation
This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.
Python3
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
51 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
52for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
51 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
75for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
34=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
55s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9 =
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
39 ________ 140 & nbsp; for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
41for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
42=
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
63for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
64for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
65s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
9 =
2for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
47 ________ 140 & nbsp; for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
49for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
50Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp;
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]