Given a string, the task is to rearrange the letters of a string in Alphabetical order in Python.
Examples:
Example1:
Input:
Given string = btechgeeks
Output:
The original string is [ btechgeeks ] The modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]
- Python Program to Split a String at First Instance of a Character
- 4 Simple Methods for Appending Multiple Strings in Python
- 4 Methods for Creating Python Multiline Strings
Example2:
Input:
Given string =Good morning this is BTechgeeks
Output:
Enter some random string = Good morning this is BTechgeeks The original string is [ Good morning this is BTechgeeks ] The modified string after reordering the letters of a string alphabetically is [ BGTcdeeegghhiiikmnnooorssst ]
Program to Rearrange the Letters of a String in Alphabetical Order in Python
Below are the ways to rearrange the letters of a string in Alphabetical order in Python.
- Using sorted() and join() functions (Static Input)
- Using sorted() and join() functions (User Input)
- Using sorted() and lambda expression (Static Input)
- Using sorted() and lambda expression (User Input)
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Method #1: Using sorted() and join() functions (Static Input)
i)Single String
Approach:
- Give the string as static input and store it in variable.
- Reorder the letters of a string alphabetically using the sorted function(This method returns a list of letters in alphabetical order).
- Join this using the join() function.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in variable. gvnstrng = 'btechgeeks' # Reorder the letters of a string alphabetically using # the sorted function(This method returns a list of letters in alphabetical order). sortdstrng = sorted(gvnstrng) # Join this using the join() function. finalstrng = ''.join(sortdstrng) # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
The original string is [ btechgeeks ] The modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]
ii)Multiple strings separated by spaces
Approach:
- Give the string as static input and store it in variable.
- Reorder the letters of a string alphabetically using the sorted function(This method returns a list of letters in alphabetical order).
- Join this using the join() function.
- Use the strip() function to remove spaces between the strings.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in variable. gvnstrng = 'Hello this is BTechgeeks' # Reorder the letters of a string alphabetically using # the sorted function(This method returns a list of letters in alphabetical order). sortdstrng = sorted(gvnstrng) # Join this using the join() function. # Use the strip() function to remove spaces between the strings. finalstrng = ''.join(sortdstrng).strip() # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
The original string is [ Hello this is BTechgeeks ] The modified string after reordering the letters of a string alphabetically is [ BHTceeeeghhiikllossst ]
Method #2: Using sorted() and join() functions (User Input)
i)Single String
Approach:
- Give the string as user input using the input() function and store it in variable.
- Reorder the letters of a string alphabetically using the sorted function(This method returns a list of letters in alphabetical order).
- Join this using the join() function.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvnstrng = input('Enter some random string = ') # Reorder the letters of a string alphabetically using # the sorted function(This method returns a list of letters in alphabetical order). sortdstrng = sorted(gvnstrng) # Join this using the join() function. finalstrng = ''.join(sortdstrng) # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
Enter some random string = btechgeeks The original string is [ btechgeeks ] The modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]
ii)Multiple strings separated by spaces
Approach:
- Give the string as user input using the input() function and store it in variable.
- Reorder the letters of a string alphabetically using the sorted function(This method returns a list of letters in alphabetical order).
- Join this using the join() function.
- Use the strip() function to remove spaces between the strings.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvnstrng = input('Enter some random string = ') # Reorder the letters of a string alphabetically using # the sorted function(This method returns a list of letters in alphabetical order). sortdstrng = sorted(gvnstrng) # Join this using the join() function. # Use the strip() function to remove spaces between the strings. finalstrng = ''.join(sortdstrng).strip() # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
Enter some random string = Good morning this is BTechgeeks The original string is [ Good morning this is BTechgeeks ] The modified string after reordering the letters of a string alphabetically is [ BGTcdeeegghhiiikmnnooorssst ]
Method #3: Using sorted() and lambda expression (Static Input)
This procedure is similar to the last one. The only difference is that we use a lambda expression with the reduce() function to connect the strings here.
i)Single String
Approach:
- Import the reduce from functools using the import Keyword.
- Give the string as static input and store it in variable.
- Use the reduce and sorted() function in a lambda expression and store it in a variable.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# import the reduce from functools using the import Keyword from functools import reduce # Give the string as user input using the input() function and store it in a variable. gvnstrng = 'btechgeeks' # Use the reduce and sorted() function in a lambda expression and store it in a variable. finalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng))) # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
The original string is [ btechgeeks ] The modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]
ii)Multiple strings separated by spaces
Approach:
- Import the reduce from functools using the import Keyword.
- Give the string as static input and store it in variable.
- Use the reduce and sorted() function in a lambda expression and store it in a variable.
- Use the strip() function to remove spaces between the strings.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# import the reduce from functools using the import Keyword from functools import reduce # Give the string as user input using the input() function and store it in a variable. gvnstrng = 'this is BTechgeeks' # Use the reduce and sorted() function in a lambda expression and store it in a variable. # Use the strip() function to remove spaces between the strings. finalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng))).strip() # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
The original string is [ this is BTechgeeks ] The modified string after reordering the letters of a string alphabetically is [ BTceeeghhiikssst ]
Method #4: Using sorted() and lambda expression (User Input)
This procedure is similar to the last one. The only difference is that we use a lambda expression with the reduce() function to connect the strings here.
i)Single String
Approach:
- Import the reduce from functools using the import Keyword.
- Give the string as user input using the input() function and store it in variable.
- Use the reduce and sorted() function in a lambda expression and store it in a variable.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# import the reduce from functools using the import Keyword from functools import reduce # Give the string as user input using the input() function and store it in a variable. gvnstrng = input('Enter some random string = ') # Use the reduce and sorted() function in a lambda expression and store it in a variable. finalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng))) # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
The original string is [ btechgeeks ] The modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]
ii)Multiple strings separated by spaces
Approach:
- Import the reduce from functools using the import Keyword.
- Give the string as user input using the input() function and store it in variable.
- Use the reduce and sorted() function in a lambda expression and store it in a variable.
- Use the strip() function to remove spaces between the strings.
- Print the modified string after reordering the letters of a string alphabetically.
- The Exit of the Program.
Below is the implementation:
# import the reduce from functools using the import Keyword from functools import reduce # Give the string as user input using the input() function and store it in a variable. gvnstrng = input('Enter some random string = ') # Use the reduce and sorted() function in a lambda expression and store it in a variable. # Use the strip() function to remove spaces between the strings. finalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng))).strip() # Print the modified string after reordering the letters of a string alphabetically. print('The original string is [', gvnstrng, ']') print( 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')
Output:
Enter some random string = Good morning this is BTechgeeks The original string is [ Good morning this is BTechgeeks ] The modified string after reordering the letters of a string alphabetically is [ BGTcdeeegghhiiikmnnooorssst ]
Related Programs:
- python program to calculate the number of digits and letters in a string
- python program to sort a list of tuples in increasing order by the last element in each tuple
- python program to count the frequency of words appearing in a string using a dictionary
- python program to print all permutations of a string in lexicographic order without recursion
- python program to calculate the number of words and the number of characters present in a string
- python program to remove the characters of odd index values in a string
- python program to accept a sentence and print only the first letter of each word in capital letters separated by a full stop