Random Palindrome Generator

  • Share this:

Code introduction


This function generates a random palindrome string based on the input parameter. A palindrome string is created by randomly selecting letters to form half of the string, then reversing it and connecting it with the other half.


Technology Stack : The Blaze third-party library is not used, packages and technologies used include: random (for generating random numbers), string (for accessing string constants)

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
def random_palindrome(length):
    import random
    import string
    
    def generate_half_palindrome(length):
        return ''.join(random.choice(string.ascii_lowercase) for _ in range(length // 2))
    
    half_palindrome = generate_half_palindrome(length)
    return half_palindrome + half_palindrome[::-1]