Conditional statement to insert middle name variable in the print statement, OR, strip white space between first name and middle name variables?

Viewed 278

It’s either remove middleName variable from the print statement and insert it between the firstName and lastName variables in the print statement on the condition (conditional statement) when a user enters a middle name, OR, leave middleName variable in the print statement and strip the white space between firstName and middleName variables when a user does not enter a middle name, because as the code is now, a huge space is left between the first and last name in the output when a user does not enter a middle name. How do I code these conditional processes; would like the option to use either? I’ve searched elsewhere for the code to no avail.

Here's my code:

import re

print('Political Elections Voter Eligibility\n\n')

pattern1 = "^[A-Z]|^[A-Z]+'[A-Z]|^[A-Z]+-[A-Z]"
pattern2 = "^[A-Z]|^[A-Z].|^[A-Z]+'[A-Z]|^[A-Z]+-[A-Z]"
pattern3 = "^[A-Z]|^[A-Z]+'[A-Z]|^[A-Z]+-[A-Z]|^[A-Z]+ [A-Z]|^[A-Z]+ [A-Z]."

while True:
    
    firstName = input("Enter your first name and tap or click 'Enter': ")
    
    print('\n')

    if re.search(pattern1, firstName):
        
        break

    else:
        
        print('\n')
        
        print('Invalid entry.')

while True:
    
    middleName = input("Enter your middle name or middle initial and tap or click 'Enter' (Optional): ")

    print('\n')

    if len(middleName)==0 or re.search(pattern2, middleName):

        break

    else:
        
        print('\n')

        print('Invalid entry.')

while True:
    
    lastName = input("Enter your last name and tap or click 'Enter': ")

    print('\n')

    if re.search(pattern3, lastName):
        
        break

    else:
        
        print('\n')

        print('Invalid entry.')

while True:
    
    try:
        age = int(input("Enter your age and tap or click 'Enter': "))

    except ValueError:
        
        print('\n')
        
        print('Invalid entry.')
        
        continue

    print('\n')

    if 1 <= age <= 125:
        
        if 1 <= age <= 17:
            print('%s %s %s, age %s, you are not eligible to vote in political elections.' % (firstName, middleName, lastName, age))
            
            break

        if 18 <= age <= 125:
            print('%s %s %s, age %s, you are eligible to vote in political elections.' % (firstName, middleName, lastName, age))
            
            break

    else:
        print('Invalid entry.')
3 Answers

The canonical way is to join all three names and then use split and join to remove multiple spaces:

fullName = ' '.join([firstName, middleName, lastName])
printName = ' '.join(fullName.split())
print(printName)

This will handle any leading or trailing whitespace, and works with or without a middle name.

I think the easiest way would be to concatenate each name string into a single string and print them all at once.

fullName = firstName + ' '  + middleName + (' ' if bool(middleName) else '')  + lastName
print('%s, age %s, you are not eligible to vote in political elections.' % (fullName, age))

The expression bool(middleName) will be evaluated as false if middleName is an empty string.

Also works:

#(' ' if len(middleName)>0 else '')

    fullName = firstName + ' ' + middleName + (' ' if len(middleName)>0 else '')  + lastName
    print(f"{fullName}, age {age}, you are not eligible to vote in political elections.")
Related