Python Program to Rearrange the Letters of a String in Alphabetical Order

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 ]

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.

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: