In the previous article, we have discussed Python Program to Find Longest Word from Sentence
Given a string and the task is to sort all words in a given sentence in alphabetic order.
sort() method :
By default, the sort() method sorts the list in ascending order.
split() method:
The split() method in Python divides a string into a list of strings by breaking the string with the specified separator.
Examples:
Example1:
Input:
Given String = "Hello this is btechgeeks"
Output:
The all sorted words of given sentence in Alphabetic order : Hello btechgeeks is this
Example2:
Input:
Given String = "Good morning this is btechgeeks"
Output:
The all sorted words of given sentence in Alphabetic order : Good btechgeeks is morning this
Program to Sort words in Alphabetic Order
Below are the ways to sort all words in a given sentence in alphabetic order.
Method #1: Using sort() Method (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
- Sort the above-obtained word list using the sort() method.
- Loop in the above list “wrd_lst” using the for loop.
- Print the iterator value to sort all words in a given sentence in alphabetic order.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable.
gven_str = "Hello this is btechgeeks"
# Split the given string into list of words using the split() function and store
# it in another variable say "wrd_lst".
wrd_lst = gven_str.split()
# Sort the above obtained word list using the sort() method.
wrd_lst.sort()
# Loop in the above list "wrd_lst" using the for loop.
print("The all sorted words of given sentence in Alphabetic order :")
for wrd in wrd_lst:
# Print the iterator value to sort all words in a given sentence in alphabetic order.
print(wrd)
Output:
The all sorted words of given sentence in Alphabetic order : Hello btechgeeks is this
Method #2: Using sort() Method (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
- Sort the above-obtained word list using the sort() method.
- Loop in the above list “wrd_lst” using the for loop.
- Print the iterator value to sort all words in a given sentence in alphabetic order.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using input() function and store it in a variable.
gven_str = input("Enter some random string = ")
# Split the given string into list of words using the split() function and store
# it in another variable say "wrd_lst".
wrd_lst = gven_str.split()
# Sort the above obtained word list using the sort() method.
wrd_lst.sort()
# Loop in the above list "wrd_lst" using the for loop.
print("The all sorted words of given sentence in Alphabetic order :")
for wrd in wrd_lst:
# Print the iterator value to sort all words in a given sentence in alphabetic order.
print(wrd)
Output:
Enter some random string = Good morning this is btechgeeks The all sorted words of given sentence in Alphabetic order : Good btechgeeks is morning this
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
