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

0

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

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

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

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.6192 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

    Input: s = "This is a python language"
    Output: This is python language
    
    Input: s = "i am laxmi"
    Output: am
    9
    This
    is
    python
    language
    5=
    This
    is
    python
    language
    7
    This
    is
    python
    language
    8
    phrase = input("Enter a phrase: ")
    words = phrase.split()  # ['I', 'need', 'practice']
    for word in words:
      print(word)
    
    4

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

    .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___

    \n1

    replace5This is python language03048

    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 thế nào để bạn in một câu từ một từ trong Python?

    Đầ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ừ.

    Làm thế nào để bạn sử dụng lệnh in trong Python?

    Python print () hàm hàm in () in thông báo được chỉ định vào màn hình hoặc thiết bị đầu ra tiêu chuẩn khác.Thông báo có thể là một chuỗi hoặc bất kỳ đối tượng nào khác, đối tượng sẽ được chuyển đổi thành một chuỗi trước khi được ghi vào màn hình.The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

    Làm thế nào để bạn hiển thị đầu ra trong Python?

    Python cung cấp hàm in () để hiển thị đầu ra cho các thiết bị đầu ra tiêu chuẩn.Trả về: Nó trả về đầu ra cho màn hình.print() function to display output to the standard output devices. Returns: It returns output to the screen.

    Làm thế nào để bạn viết các từ trong mã python?

    Trong Python, bạn có thể ghi vào tệp văn bản bằng cách làm theo ba bước sau: Mở tệp bằng hàm Open ().Open a file using the open() function. Write to the file using the write() method.. Close the file using the close() method.