Hướng dẫn how do i print one word at a time in python? - làm cách nào để in từng từ một trong python?

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

  • Đọc
  • Bàn luận
  • 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]
    
    2
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    3
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    4

    Cho 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: am
    9
    This
    is
    python
    language
    0
    Input: s = "This is a python language"
    Output: This is python language
    
    Input: s = "i am laxmi"
    Output: am
    2

    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    5
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    6
    phrase = 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]
    
    8

    This
    is
    python
    language

    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    9
    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
    1
    Input: s = "This is a python language"
    Output: This is python language
    
    Input: s = "i am laxmi"
    Output: am
    2223
    Input: 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
    language
    2
    This
    is
    python
    language
    3

    .split[]5

    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
    1.split[]8
    Input: s = "This is a python language"
    Output: This is python language
    
    Input: s = "i am laxmi"
    Output: am
    3
    Input: s = "This is a python language"
    Output: This is python language
    
    Input: s = "i am laxmi"
    Output: am
    4___

    replace5

    This
    is
    python
    language
    03048

    This
    is
    python
    language
    5= \n0

    \n1

    Phương pháp: Sử dụng chức năng Lambda

    Python3

    n=\n4

    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]
    
    2
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    3
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    4

    n0=__72

    This
    is
    python
    language
    0=7

    Phương pháp: Sử dụng danh sách hiểu

    Python3

    n=\n4

    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]
    
    2
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    3
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    4

    This
    is
    python
    language
    0"This is a python language"7
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    5 "This is a python language"9
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    7

    Phương pháp: Sử dụng hàm liệt kê & nbsp;

    Python3

    n=\n4

    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]
    
    2
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    3
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    4

    This
    is
    python
    language
    0"This is a python language"7
    phrase = input["Enter a phrase: "]
    words = phrase.split[]  # ['I', 'need', 'practice']
    for word in words:
      print[word]
    
    5


    Làm cách nào để in chỉ một từ trong Python?

    Approach..
    Chia chuỗi thành các từ, sử dụng str. Chia [] chức năng ..
    Lặp lại qua các từ ..
    Tính độ dài của từ sử dụng hàm len [] ..
    Nếu độ dài là chẵn, hãy in từ ..
    Nếu không thì không làm gì cả ..

    Làm thế nào để bạn in một từ trong Python?

    Hàm python print [] có bất kỳ số lượng tham số nào và in chúng ra trên một dòng văn bản.Các mục được chuyển đổi thành mẫu văn bản, được phân tách bằng khoảng trắng và có một '\ n' ở cuối [char "newline"].Khi được gọi với các tham số bằng không, print [] chỉ in '\ n' và không có gì khác.. The items are each converted to text form, separated by spaces, and there is a single '\n' at the end [the "newline" char]. When called with zero parameters, print[] just prints the '\n' and nothing else.

    Làm cách nào để in một chữ cái tại một thời điểm trong Python?

    In một ký tự tại một thời điểm trên cùng một dòng trong Python #..
    Sử dụng một vòng lặp để lặp qua chuỗi ..
    Sử dụng hàm in [] để in từng ký tự ..
    Đặt đối số kết thúc trong cuộc gọi để in [] thành một chuỗi trống ..

    Làm cách nào để in một số từ nhất định trong một chuỗi?

    Một phương pháp thực hiện điều này là tách/chuyển đổi chuỗi thành mảng ký tự và sau đó in các ký tự mà bạn muốn sử dụng chỉ mục ký tự bắt đầu của chuỗi đó.Hoặc nếu không bạn có thể sử dụng phương thức Split [] bằng cách sử dụng mà bạn có thể in các từ riêng lẻ.splitting/converting the string into character array and then print the characters that you want using the index of starting character of that string. Or else you can use the split[] method using which you can print the individual words.

    Bài Viết Liên Quan

    Chủ Đề