Hướng dẫn reverse the sentence in python - đảo ngược câu trong python

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input : str =" geeks quiz practice code"
    Output : str = code practice quiz geeks  
    Input : str = "my name is laxmi"
    output : str= laxmi is name my 

    Bàn luận

    Python3

    Chúng tôi được cung cấp một chuỗi và chúng tôi cần đảo ngược các từ của một chuỗi đã cho

    Đảo ngược các từ trong chương trình chuỗi đã cho & nbsp;

    code practice quiz geeks
    2
    code practice quiz geeks
    3
    code practice quiz geeks
    4

    code practice quiz geeks
    5
    code practice quiz geeks
    3
    code practice quiz geeks
    7
    code practice quiz geeks
    8
    code practice quiz geeks
    9
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    0

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    8
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    9

    code practice quiz geeks
    0
    code practice quiz geeks
    1
    code practice quiz geeks
    2
    code practice quiz geeks
    3

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    1
    code practice quiz geeks
    3
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    3

    code practice quiz geeks

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    4
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    5
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    6
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    7
    O(n), where n is the length of the string
    Auxiliary Space: O(n), where n is the length of the string

    Đầu ra

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks

    Độ phức tạp về thời gian: O (n), trong đó n là độ dài của không gian chuỗi chuỗi

    • Chúng ta được cung cấp một chuỗi và chúng ta cần đảo ngược các từ của một chuỗi đã cho? Ví dụ:
    • Vấn đề này có một giải pháp hiện có, vui lòng giới thiệu các từ ngược trong một liên kết chuỗi đã cho. Chúng tôi sẽ giải quyết vấn đề này trong Python. Đưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này.
    • Tách từng từ trong một chuỗi đã cho bằng phương thức Split () của Kiểu dữ liệu chuỗi trong Python.

    Implementation:

    Python3

    Đảo ngược danh sách phân tách từ.

    In các từ của danh sách, ở dạng chuỗi sau khi nối từng từ với không gian bằng phương thức.

    code practice quiz geeks
    4
    code practice quiz geeks
    5

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    8
    code practice quiz geeks
    7
    code practice quiz geeks
    3
    code practice quiz geeks
    9
    code practice quiz geeks
    0
    code practice quiz geeks
    1

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    8
    code practice quiz geeks
    3
    code practice quiz geeks
    3
    code practice quiz geeks
    0
    code practice quiz geeks
    6
    code practice quiz geeks
    7
    code practice quiz geeks
    8

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    8
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    0
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    1

    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    2
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    3
    code practice quiz geeks
    3
    code practice quiz geeks
    3
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    6
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    7

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    1
    code practice quiz geeks
    3
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    3

    code practice quiz geeks

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    4
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    5
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    6
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    7
    O(n), where n is the length of the string
    Auxiliary Space: O(n), where n is the length of the string

    Đầu ra

    • Độ phức tạp về thời gian: O (n), trong đó n là độ dài của không gian chuỗi chuỗi
    • Chúng ta được cung cấp một chuỗi và chúng ta cần đảo ngược các từ của một chuỗi đã cho? Ví dụ:
    • Vấn đề này có một giải pháp hiện có, vui lòng giới thiệu các từ ngược trong một liên kết chuỗi đã cho. Chúng tôi sẽ giải quyết vấn đề này trong Python. Đưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này.

    Python3

    Tách từng từ trong một chuỗi đã cho bằng phương thức Split () của Kiểu dữ liệu chuỗi trong Python.

    Đảo ngược danh sách phân tách từ.

    In các từ của danh sách, ở dạng chuỗi sau khi nối từng từ với không gian bằng phương thức.

    code practice quiz geeks
    4
    code practice quiz geeks
    5

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    8
    code practice quiz geeks
    7
    code practice quiz geeks
    3
    code practice quiz geeks
    9
    code practice quiz geeks
    0
    code practice quiz geeks
    1

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    8
    code practice quiz geeks
    3
    code practice quiz geeks
    3
    code practice quiz geeks
    0
    code practice quiz geeks
    6
    code practice quiz geeks
    7
    code practice quiz geeks
    8

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    8
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    0
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    1

    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    2
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    3
    code practice quiz geeks
    3
    code practice quiz geeks
    3
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    6
    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)
    7

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    1
    code practice quiz geeks
    3
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    3

    code practice quiz geeks

    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    4
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    5
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    6
    Input : str = geeks quiz practice code
    Output : str = code practice quiz geeks
    7Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.


    Đầu ra

    Độ phức tạp về thời gian: O (n), trong đó n là độ dài của không gian chuỗi chuỗi

    Chúng ta được cung cấp một chuỗi và chúng ta cần đảo ngược các từ của một chuỗi đã cho? Ví dụ:

    1. Vấn đề này có một giải pháp hiện có, vui lòng giới thiệu các từ ngược trong một liên kết chuỗi đã cho. Chúng tôi sẽ giải quyết vấn đề này trong Python. Đưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này.
    2. Tách từng từ trong một chuỗi đã cho bằng phương thức Split () của Kiểu dữ liệu chuỗi trong Python.
    3. Đảo ngược danh sách phân tách từ.

    In các từ của danh sách, ở dạng chuỗi sau khi nối từng từ với không gian bằng phương thức.

    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = word_list[:: -1]
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)

    Đầu ra

    arrives still destiny it from run it dread

    Chương trình này có thể được nén thêm.

    sentence = "dread it run from it destiny still arrives"
    print(" ".join(sentence.split()[::-1]))

    Đầu ra

    arrives still destiny it from run it dread

    Danh sách Python có thể được đảo ngược bằng phương pháp

    code practice quiz geeks
    16, có thể được sử dụng thay cho
    code practice quiz geeks
    17 trong chương trình như sau.

    sentence = "dread it run from it destiny still arrives"
    word_list = sentence.split()
    reversed_list = reversed(word_list)
    reversed_sentence = " ".join(reversed_list)
    print(reversed_sentence)

    Chương trình cho đầu vào do người dùng cung cấp

    code practice quiz geeks
    0

    Đầu ra

    code practice quiz geeks
    1

    Chương trình