Tôi đang cố gắng in một chuỗi một từ trên mỗi dòng bằng một vòng lặp. Ví dụ: nếu chuỗi là, 'Tôi cần thực hành', chuỗi nên in: i '\ n' cần '\ n' thực hành
Mã của tôi cho đến nay trông như thế này:
phrase=input['enter a phrase: ']
for char in phrase:
print [char, end ='']
if char == '':
print['\n']
Tuy nhiên, đầu ra của tôi trông như thế này:
tôi cần luyện tập
kiyah
1.4861 Huy hiệu vàng19 Huy hiệu bạc27 Huy hiệu đồng1 gold badge19 silver badges27 bronze badges
Đã hỏi ngày 2 tháng 3 năm 2018 lúc 22:39Mar 2, 2018 at 22:39
1
Bạn có thể sử dụng chức năng .split[]
để chia một chuỗi theo không gian và điều đó sẽ cung cấp cho bạn một danh sách các từ mà sau đó bạn có thể lặp và in chúng ra.
Hàm chấp nhận một chuỗi sẽ được sử dụng làm dấu phân cách [ví dụ: ","] nếu đối số này không được chỉ định hoặc không có nó sẽ chạy một thuật toán sẽ xem xét một chuỗi khoảng trắng như một dấu phân cách và do đó, do đó, do đó, do đó, do đó Cho dù có bao nhiêu khoảng trống giữa các từ, bạn sẽ nhận được một danh sách các từ không có khoảng trắng ở đầu hoặc cuối mỗi chuỗi con.if this argument is not specified or is None it will run an algorithm that will consider a sequence of whitespaces as a single separator and hence, as a result, no matter how many spaces there are between the words you will get a list of words with no whitespaces at the start or the end of each substring.
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
Đã trả lời ngày 2 tháng 3 năm 2018 lúc 22:41Mar 2, 2018 at 22:41
RafaelrafaelRafael
6.7225 huy hiệu vàng41 Huy hiệu bạc48 Huy hiệu đồng5 gold badges41 silver badges48 bronze badges
Chỉ replace
các không gian với \n
's
Cụm từ = đầu vào ['Nhập một cụm từ:']
Words = cụm từ.replace ['', '\ n']]
Lưu ý không gian trong đối số đầu tiên của thay thế.
Đã trả lời ngày 2 tháng 3 năm 2018 lúc 22:46Mar 2, 2018 at 22:46
jath03jath03jath03
1.6392 Huy hiệu vàng14 Huy hiệu bạc20 Huy hiệu Đồng2 gold badges14 silver badges20 bronze badges
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am
Bàn luậnFinding 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"
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
0=
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
2phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
3phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
4Cho một chuỗi. Nhiệm vụ là in tất cả các từ có độ dài chẵn trong chuỗi đã cho.
Phương pháp: Tìm từ chẵn các từ sử dụng cho vòng lặp và câu lệnh nếu không sử dụng hàm def. Đầu tiên phân chia chuỗi đã cho bằng cách sử dụng hàm chia [] và sau đó lặp lại các từ của chuỗi bằng cách sử dụng vòng lặp. Tính độ dài của mỗi từ sử dụng hàm Len []. Nếu độ dài là chẵn, sau đó in từ.
Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am9
This is python language0
Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am2
5 phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
6phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
7 phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
8phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
This is python language
9phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
Input: s = "This is a python language"
Output: This is python language
Input: s = "i am laxmi"
Output: am
0 Input: s = "This is a python language"
Output: This is python language
Input: s = "i am laxmi"
Output: am
1Input: s = "This is a python language"
Output: This is python language
Input: s = "i am laxmi"
Output: am
2223Input: s = "This is a python language"
Output: This is python language
Input: s = "i am laxmi"
Output: am
4___ 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
Đầu ra
Cách tiếp cận: Chia chuỗi bằng cách sử dụng hàm split []. Lặp lại trong các từ của một chuỗi sử dụng cho vòng lặp. Tính độ dài của từ sử dụng hàm len []. Nếu độ dài là chẵn, sau đó in từ. Dưới đây là việc thực hiện Python của phương pháp trên: & NBSP;
This is python language2
This is python language3
.split[]
5
Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am0
Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am1
.split[]
8Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am3
Input: s = "This is a python language" Output: This is python language Input: s = "i am laxmi" Output: am4___
replace
5
This is python language03048
This is python language5
=
\n
0\n
1
Phương pháp: Sử dụng chức năng Lambda
Python3
n
=
\n
4
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
0=
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
2phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
3phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
4n
0=
__72
This is python language0
=
7Phương pháp: Sử dụng danh sách hiểu
Python3
n
=
\n
4
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
0=
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
2phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
3phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
4This is python language0
"This is a python language"
7phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
5 "This is a python language"
9phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
7Phương pháp: Sử dụng hàm liệt kê & nbsp;
Python3
n
=
\n
4
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
0=
phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
2phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
3phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
4This is python language0
"This is a python language"
7phrase = input["Enter a phrase: "]
words = phrase.split[] # ['I', 'need', 'practice']
for word in words:
print[word]
5